Jump to content

bindTheKeys


fairyoggy

Recommended Posts

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
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
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
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 by The_GTA
Link to comment
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

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 )

 

  • Like 1
Link to comment
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 :P 

But I strongly discourage: slapztea should not use the same function for binding keys and event handling.

Edited by The_GTA
  • Like 1
Link to comment
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
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? :) 

  • Like 1
Link to comment
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 by #\_oskar_/#
  • Like 1
Link to comment
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

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...