Jump to content

cancelEvent onPlayerWeaponFire


Recommended Posts

Hi, I'm trying to make it impossible to shoot a weapon unless you're aiming it, but I can't figure out how to do it properly.

-- serverside.lua

function isPlayerAiming()
	if getControlState(source, "aim_weapon") then
		outputChatBox("aiming", source)
	else
		outputChatBox("not aiming", source)
		cancelEvent() -- should not shoot the weapon if not aiming
	end
end

addEventHandler("onPlayerWeaponFire", getRootElement(), isPlayerAiming)

It does output the correct message when I'm shooting the weapon, but I don't know how to cancel the event when I'm not aiming. Am I using the wrong event or am I just doing this wrong?

Link to comment
  • Moderators

Try this:

function isPlayerAiming()
	if getControlState(source, "aim_weapon") then
		if not (isControlEnabled(source, "fire")) then
			toggleControl( source, "fire", true )
		end
	else
		if isControlEnabled(source, "fire") then
			toggleControl(source, "fire", false)
		end
	end
end

addEventHandler("onPlayerWeaponFire", getRootElement(), isPlayerAiming)

 

Edited by DNL291
Link to comment
4 minutes ago, DNL291 said:

Try this:


function isPlayerAiming()
	if getControlState(source, "aim_weapon") then
		if not (isControlEnabled(source, "fire")) then
			toggleControl( source, "fire", true )
		end
	else
		if isControlEnabled(source, "fire") then
			toggleControl(source, "fire", false)
		end
	end
end

addEventHandler("onPlayerWeaponFire", getRootElement(), isPlayerAiming)

 

I've already tried doing this, but it permanently disables the fire control after I shoot once and basically the only way to turn it back on is to shoot while aiming, which is impossible because shooting is now disabled.

I also tried setControlState (which you wrote before editing) and it stops firing after the first bullet, but if I click again I can shoot again and again.

I guess I must use setControlState(source, "fire", false) before the event actually happens somehow?

Link to comment

EDIT: I managed to do it properly clientside, thank you for the help.

 

--clientside

function cancelShooting(key, press)
	local keys = getBoundKeys ( "fire" )
	for keyName, state in pairs(keys) do
		if key == keyName and press then
			if getControlState("aim_weapon") then
				outputChatBox("aiming")
			else
				outputChatBox("not aiming")
				cancelEvent()
			end
		end
	end
end	


addEventHandler("onClientKey", getRootElement(), cancelShooting)



 

Edited by UnchiuDawg
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...