fairyoggy Posted March 29, 2020 Share Posted March 29, 2020 if you type the command into the chat /engine then it works as it should, but how to make the necessary function run with the resource without a command? I tried to do through onResourceStart but an error appears and I don’t understand how to solve it. how to fix it? --server side function bindTheKeys ( playerSource, commandName ) bindKey ( playerSource, "L", "down", switchEngine ) end addCommandHandler("engine",bindTheKeys) addEventHandler ( "onResourceStart", getRootElement(), bindTheKeys) error line: addEventHandler ( "onResourceStart", getRootElement(), bindTheKeys) Bad Argument @ "addEventHandler" [Expected element at argument 2, got nill] Link to comment
The_GTA Posted March 29, 2020 Share Posted March 29, 2020 11 minutes ago, slapztea said: error line: addEventHandler ( "onResourceStart", getRootElement(), bindTheKeys) Bad Argument @ "addEventHandler" [Expected element at argument 2, got nill] This is a very strange error. Did you replace the "getRootElement" function? Can you share the entire resource scripts? What about the global "root" variable instead of calling "getRootElement"? Link to comment
fairyoggy Posted March 29, 2020 Author Share Posted March 29, 2020 38 minutes ago, The_GTA said: This is a very strange error. Did you replace the "getRootElement" function? Can you share the entire resource scripts? What about the global "root" variable instead of calling "getRootElement"? I, apparently, was mistaken with an error Error: Expected player at argument 1, got resource-data Is this another mistake? Link to comment
The_GTA Posted March 29, 2020 Share Posted March 29, 2020 (edited) 1 minute ago, slapztea said: I, apparently, was mistaken with an error Error: Expected player at argument 1, got resource-data Is this another mistake? I cannot see what line of code this refers to, thus I assume this is an entirely different issue. Could you post the related code? Edited March 29, 2020 by The_GTA Link to comment
fairyoggy Posted March 29, 2020 Author Share Posted March 29, 2020 7 minutes ago, The_GTA said: I cannot see what line of code this refers to, thus I assume this is an entirely different issue. Could you post the related code? Error on the same line addEventHandler ( "onResourceStart", getRootElement(), bindTheKeys) if use root instead getRootElement() the error is the same. so here is what i have --server function switchEngine ( playerSource) local theVehicle = getPedOccupiedVehicle ( playerSource ) if theVehicle and getVehicleController ( theVehicle ) == playerSource then local uid = getElementData(theVehicle, "uid") local state = getVehicleEngineState(theVehicle) outputChatBox("Driver", playerSource, 220, 220, 0) setVehicleEngineState(theVehicle,true) elseif not theVehicle then outputChatBox("you are not in car", playerSource, 220, 220, 0) else outputChatBox("Not driver", playerSource, 220, 220, 0) end end addEvent ( "switchEngine", true ) addEventHandler( "switchEngine", root, switchEngine ) function bindTheKeys ( playerSource, commandName ) bindKey ( playerSource, "L", "down", switchEngine ) end addCommandHandler("engine",bindTheKeys) addEventHandler ( "onResourceStart", getRootElement(), bindTheKeys) Link to comment
Bartje Posted March 29, 2020 Share Posted March 29, 2020 I am not entirely sure, but it seems like you are trying to bind the keys to the server instead of the player. Maybe you are looking for another eventHandler. What about: addEventHandler ( "onPlayerJoin", getRootElement(), bindTheKeys ) 1 Link to comment
The_GTA Posted March 29, 2020 Share Posted March 29, 2020 (edited) 4 minutes ago, Bartje said: I am not entirely sure, but it seems like you are trying to bind the keys to the server instead of the player. Maybe you are looking for another eventHandler. What about: addEventHandler ( "onPlayerJoin", getRootElement(), bindTheKeys ) Wow, I must have been pretty tired. True, that is the problem OP was talking about But I strongly discourage: slapztea should not use the same function for binding keys and event handling. Edited March 29, 2020 by The_GTA 1 Link to comment
fairyoggy Posted March 29, 2020 Author Share Posted March 29, 2020 15 minutes ago, Bartje said: I am not entirely sure, but it seems like you are trying to bind the keys to the server instead of the player. Maybe you are looking for another eventHandler. What about: addEventHandler ( "onPlayerJoin", getRootElement(), bindTheKeys ) This does not work, I even tried another event(OnPlayerLogin), but the effect is the same. It doesn’t work, but as soon as I type the command into the chat (/engine) then everything works bindKey ( playerSource, "L", "down", switchEngine ) BadArgument 'binkKey' [Expected player at argument 1, got account Link to comment
The_GTA Posted March 29, 2020 Share Posted March 29, 2020 15 minutes ago, slapztea said: This does not work, I even tried another event(OnPlayerLogin), but the effect is the same. It doesn’t work, but as soon as I type the command into the chat (/engine) then everything works BadArgument 'binkKey' [Expected player at argument 1, got account No idea how you got an error message about "account" but here is my attempt at fixing your script: function switchEngine ( playerSource) local theVehicle = getPedOccupiedVehicle ( playerSource ) if theVehicle and getVehicleController ( theVehicle ) == playerSource then local uid = getElementData(theVehicle, "uid") local state = getVehicleEngineState(theVehicle) outputChatBox("Driver", playerSource, 220, 220, 0) setVehicleEngineState(theVehicle,true) elseif not theVehicle then outputChatBox("you are not in car", playerSource, 220, 220, 0) else outputChatBox("Not driver", playerSource, 220, 220, 0) end end addEvent ( "switchEngine", true ) addEventHandler( "switchEngine", root, switchEngine ) function bindTheKeys (playerSource) bindKey ( playerSource, "L", "down", switchEngine ) end addCommandHandler("engine",bindTheKeys) addEventHandler ( "onPlayerJoin", getRootElement(), function() bindTheKeys(source) end) I have fixed the last line because you should not use the same function for event handlers and bind key handlers. Does that fix the underlying issue? 1 Link to comment
#\_oskar_/# Posted March 29, 2020 Share Posted March 29, 2020 (edited) local Key = 'L' -- addEventHandler("onPlayerJoin",root,function () bindKey (source,Key,"down",switchEngine) end) local function bindTheKeys() for _,player in ipairs(getElementsByType("player")) do bindKey (player,Key,"down",switchEngine) end end addEventHandler("onResourceStart",resourceRoot,bindTheKeys) try Edited March 29, 2020 by #\_oskar_/# 1 Link to comment
fairyoggy Posted March 29, 2020 Author Share Posted March 29, 2020 14 minutes ago, The_GTA said: No idea how you got an error message about "account" but here is my attempt at fixing your script: function switchEngine ( playerSource) local theVehicle = getPedOccupiedVehicle ( playerSource ) if theVehicle and getVehicleController ( theVehicle ) == playerSource then local uid = getElementData(theVehicle, "uid") local state = getVehicleEngineState(theVehicle) outputChatBox("Driver", playerSource, 220, 220, 0) setVehicleEngineState(theVehicle,true) elseif not theVehicle then outputChatBox("you are not in car", playerSource, 220, 220, 0) else outputChatBox("Not driver", playerSource, 220, 220, 0) end end addEvent ( "switchEngine", true ) addEventHandler( "switchEngine", root, switchEngine ) function bindTheKeys (playerSource) bindKey ( playerSource, "L", "down", switchEngine ) end addCommandHandler("engine",bindTheKeys) addEventHandler ( "onPlayerJoin", getRootElement(), function() bindTheKeys(source) end) I have fixed the last line because you should not use the same function for event handlers and bind key handlers. Does that fix the underlying issue? you do not claim to be the best assistant on the forum? Thanks for one more resolved question! 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