Roderen Posted July 24, 2021 Share Posted July 24, 2021 function BINDkill(source, command) bindKey(source, "n", "down", function() killPed(source, source) end) end addCommandHandler("bk", BINDkill) At the moment, the bind works only after the command in the chat. How to make the bind work when the server starts? Link to comment
Hydra Posted July 24, 2021 Share Posted July 24, 2021 function killMe() killPed(source) end function onStartBindKill() bindKey(source, "n", "down", killMe) end addEventHandler("onResourceStart", resourceRoot, onStartBindKill) Link to comment
DiSaMe Posted July 25, 2021 Share Posted July 25, 2021 (edited) 14 hours ago, Hydra said: function killMe() killPed(source) end function onStartBindKill() bindKey(source, "n", "down", killMe) end addEventHandler("onResourceStart", resourceRoot, onStartBindKill) There are problems with this code. First, the source of "onResourceStart" event is the root element of that resource (same as resourceRoot, I think), but bindKey requires player as first argument. Second, the handler functions for bindKey don't have sources. Instead, the player is passed as first argument. I don't know if bindKey has call propagation (call propagation means a function call made on an element will apply to its children elements). If it does, we can pass root to the player argument of bindKey and the key binding will be added for all players: function killMe(player) killPed(player) end function onStartBindKill() bindKey(root, "n", "down", killMe) end addEventHandler("onResourceStart", resourceRoot, onStartBindKill) If bindKey doesn't have call propagation, the above code is still wrong and we have to call bindKey separately for each player. That means calling bindKey every time a player joins. But a resource might be started after some players have already joined, so we also have to call bindKey for already present players when it starts: function killMe(player) killPed(player) end function onJoinBindKill() bindKey(source, "n", "down", killMe) end addEventHandler("onPlayerJoin", root, onJoinBindKill) function onStartBindKill() local players = getElementsByType("player") for key, player in ipairs(players) do bindKey(player, "n", "down", killMe) end end addEventHandler("onResourceStart", resourceRoot, onStartBindKill) Edited July 25, 2021 by CrystalMV 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