Jump to content

Try to bind key


murilo2929

Recommended Posts

 
function funcInput ( thePlayer, key, keyState )
  triggerClientEvent(thePlayer, "enableRoadblockGUI", getRootElement(), true)
end

function bindTheKeys ( thePlayer, commandName )
  bindKey ( thePlayer, "F5", "down", funcInput )
end
addCommandHandler ( "bindme", bindTheKeys )

how do I bind this key without the player having to type the command "bindme"?

Link to comment
function bindKeysForPlayer(plr)
  bindKey(source or plr, "F5", "down", funcInput) -- bind it to the event's source (onPlayerJoin) or the first parameter if source doesn't exist (i.e. function wasn't called by an event but by script)
end
addEventHandler("onPlayerJoin", root, bindKeysForPlayer)

function bindKeysForAllPlayers()
  for k, v in pairs(getElementsByType("player")) do
    bindKeysForPlayer(v) -- call the binding function for each player
  end
end
addEventHandler("onResourceStart", resourceRoot, bindKeysForAllPlayers, false)

Unless the server code does anything else other than trigger the client event (e.g. access control), you may just as well move this whole part over to the client. That way you can just bind the key in the main scope of the file outside of any function and it'll bind as soon as the script loads. On the server, you have to use an event to bind the key when a player joins.

Link to comment
5 hours ago, MrTasty said:

function bindKeysForPlayer(plr)
  bindKey(source or plr, "F5", "down", funcInput) -- bind it to the event's source (onPlayerJoin) or the first parameter if source doesn't exist (i.e. function wasn't called by an event but by script)
end
addEventHandler("onPlayerJoin", root, bindKeysForPlayer)

function bindKeysForAllPlayers()
  for k, v in pairs(getElementsByType("player")) do
    bindKeysForPlayer(v) -- call the binding function for each player
  end
end
addEventHandler("onResourceStart", resourceRoot, bindKeysForAllPlayers, false)

Unless the server code does anything else other than trigger the client event (e.g. access control), you may just as well move this whole part over to the client. That way you can just bind the key in the main scope of the file outside of any function and it'll bind as soon as the script loads. On the server, you have to use an event to bind the key when a player joins.

function funcInput ( thePlayer, key, keyState )
  triggerClientEvent(thePlayer, "enableRoadblockGUI", getRootElement(), true)
end
addEventHandler( "funcInput", getRootElement(), funcInput )

function bindTheKeys ( thePlayer )
  bindKey ( thePlayer, "F5", "down", funcInput )
end
addEventHandler("onClientResourceStart", resourceRoot, bindTheKeys)
addEventHandler ( "onPlayerJoin", getRootElement(), bindTheKeys)

I try this on client side and no success.

Link to comment

If you want to use it on the client, you can't use functions like triggerClientEvent. As I've outlined in my post above, on the client, you can literally just add bindKey("F5", "down", func_that_handles_enableRoadblockGUI_event) outside any function and it'll bind as soon as the script loads on the client. If you prefer to use server-side, you can just use the code I gave in that other post

Edited by MrTasty
Link to comment
4 hours ago, MrTasty said:

If you want to use it on the client, you can't use functions like triggerClientEvent. As I've outlined in my post above, on the client, you can literally just add bindKey("F5", "down", func_that_handles_enableRoadblockGUI_event) outside any function and it'll bind as soon as the script loads on the client. If you prefer to use server-side, you can just use the code I gave in that other post

Got it, ty help alot

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