Jump to content

How to enable binds when the server is turned on?


Roderen

Recommended Posts

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