Noah_Antilles Posted May 18, 2017 Share Posted May 18, 2017 (edited) I've been posting a lot here lately, and I am sorry if I'm getting annoying. But after a whole afternoon of trying to get my script to work I'd like to get some feedback. After I learned of the existence of debugging I immediatly went ahead and tried it. I almost fixed my script, but there's one problem keeping it from working Context: Player enters vehicle and an uzi is spawned and attached to the vehicle When the player presses left mouse button the uzi starts firing When the left mouse button is released the uzi should stop firing. function hydraFunctions() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then outputChatBox ("Hydra guns installed") toggleControl ( "accelerate", true ) toggleControl ( "brake_reverse", true ) toggleControl ( "vehicle_secondary_fire", false ) toggleControl ( "vehicle_fire", false ) setControlState ( "special_control_up", true) toggleControl ( "special_control_down", false) local x, y, z = getElementPosition(vehicle) local weapon = createWeapon("uzi", x, y, z) local weaponHydraR = setElementData(vehicle, "weaponHydraR", weapon, true) attachElements ( weapon, vehicle, 0, 4, 1, 0, 0, 90) setWeaponClipAmmo(weapon, 500) setWeaponFiringRate(weapon, 90) end end end addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions ) function hydraFiring() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then setWeaponState(weapon, "firing") -- Can't seem to find the weapon outputChatBox ("guns activated") end end end end function hydraStopping() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then setWeaponState(weapon, "ready") -- Can't seem to find the weapon outputChatBox ("guns deactivated") end end end end bindKey("mouse1", "down", hydraFiring) bindKey("mouse1", "up", hydraStopping) bindKey("lctrl", "down", hydraFiring) bindKey("lctrl", "up", hydraStopping) WARNING: 1947weapons\hydraweaponC.lua:41: Bad argument @ 'setWeaponState' [Expected weapon at argument 1, got nil] WARNING: 1947weapons\hydraweaponC.lua:28: Bad argument @ 'setWeaponState' [Expected weapon at argument 1, got nil] What I think is happening: The uzi spawns perfectly fine, but for some reason the following functions (hydraFiring & hydraStopping) cannot "detect" the uzi, hence the [Expected weapon at argument 1, got nil] error message. The uzi is simply not found. Also, I read on the forums that the setElementData and getElementData should only be used with more important data (which this isn't really I guess.) Is there an alternative that I can use? Thanks for your time, -Noah Edited May 18, 2017 by Noah_Antilles Link to comment
^iiEcoo'x_) Posted May 18, 2017 Share Posted May 18, 2017 setWeaponState(weapon, "firing") ?? 1 Link to comment
Moderators IIYAMA Posted May 18, 2017 Moderators Share Posted May 18, 2017 (edited) Using elementdata is fine, just make sure you disable the syncronization when you don't need it. ElementData is absolute not for important information. It is very foolish to save passwords or similar data in it. bool setElementData ( element theElement, string key, var value [, bool synchronize = true ] ) You could also use: bool setElementParent ( element theElement, element parent ) element getElementParent ( element theElement ) table getElementChildren ( element parent [, string theType = nil ] ) element getElementChild ( element parent, int index ) Or learn tables. Edited May 18, 2017 by IIYAMA 1 Link to comment
Function Posted May 18, 2017 Share Posted May 18, 2017 (edited) addEventHandler("onClientClick",root, function(click,state) if click == "mouse1" and state == "up" then setWeaponState(weapon,"firing") else setWeaponState(weapon,"ready") end end ) Edited May 18, 2017 by Function 1 Link to comment
pa3ck Posted May 18, 2017 Share Posted May 18, 2017 (edited) The scope of the variable "weapon" is in the function hydraFunction as it's set to be a local variable, so hydraFiring and hydraStopping is outside the scope. You're are already saving the weapon element to an elementData, why not use that instead of the "weapon" variable? Try this: function hydraFunctions() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then outputChatBox ("Hydra guns installed") toggleControl ( "accelerate", true ) toggleControl ( "brake_reverse", true ) toggleControl ( "vehicle_secondary_fire", false ) toggleControl ( "vehicle_fire", false ) setControlState ( "special_control_up", true) toggleControl ( "special_control_down", false) local x, y, z = getElementPosition(vehicle) local weapon = createWeapon("uzi", x, y, z) local weaponHydraR = setElementData(vehicle, "weaponHydraR", weapon, true) attachElements ( weapon, vehicle, 0, 4, 1, 0, 0, 90) setWeaponClipAmmo(weapon, 500) setWeaponFiringRate(weapon, 90) end end end addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions ) function hydraFiring() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then setWeaponState(getElementData(vehicle, "weaponHydraR"), "firing") -- Can't seem to find the weapon outputChatBox ("guns activated") end end end end function hydraStopping() local vehicle = getPedOccupiedVehicle(localPlayer) if(vehicle)then if getElementModel(vehicle) == 520 then if getElementData(vehicle, "weaponHydraR", true ) then setWeaponState(getElementData(vehicle, "weaponHydraR"), "ready") -- Can't seem to find the weapon outputChatBox ("guns deactivated") end end end end bindKey("mouse1", "down", hydraFiring) bindKey("mouse1", "up", hydraStopping) bindKey("lctrl", "down", hydraFiring) bindKey("lctrl", "up", hydraStopping) Edited May 18, 2017 by pa3ck 1 Link to comment
Noah_Antilles Posted May 19, 2017 Author Share Posted May 19, 2017 Thank you for your tips and feedback. I was struggling so hard to find why the setWeaponState function didn't work ^^ Now I see the local weapon created in hydraFunctions was outside the scope and thus not recognized by the other functions. I learned some great tips and tricks thanks to you all. Thanks again! Link to comment
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