UnchiuDawg Posted May 28, 2017 Posted May 28, 2017 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?
DNL291 Posted May 28, 2017 Posted May 28, 2017 (edited) 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 May 28, 2017 by DNL291
UnchiuDawg Posted May 28, 2017 Author Posted May 28, 2017 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?
UnchiuDawg Posted May 28, 2017 Author Posted May 28, 2017 (edited) 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 May 28, 2017 by UnchiuDawg
Moderators IIYAMA Posted May 29, 2017 Moderators Posted May 29, 2017 Make sure you break the loop after cancelling the event. (performance) break 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now