Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make my function now run when it is an argument?

Asked by
tjtorin 172
6 years ago
admins = {
    ["tjtorin"] = true
}
prefix = ":"

function newCmd(plr,msg,name,code)
    if admins[plr.Name] then
        if msg == prefix..name then
            spawn(code())
        end
    end
end

--   FUNCTIONS
------------------------
function kill(plr)
    game.Players[plr.Name].Character.Head:Destroy()
end
------------------------

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        newCmd(plr,msg,"kill",kill(plr)) -- This is only supposed to run the kill(plr) if the player chatted is an admin but it runs if anyone says anything because its a function and its being called as an argument but how do I get it not to run here?
    end)
end)

1 answer

Log in to vote
0
Answered by 6 years ago

Simple. All you have to do is switch the parameters. There is no way to avoid running the kill function if you're going to include the function as an argument. Instead, do this:

admins = {
    ["tjtorin"] = true
}
prefix = ":"

function newCmd(player,msg,name,code)
    if admins[player.Name] then
        if msg == prefix..name then
            spawn(code(player)) -- place arguments inside here, AFTER the checks are made.
        end
    end
end

--   FUNCTIONS
------------------------
function kill(plr)
    if plr ~= nil then
      game.Players[plr.Name].Character.Head:Destroy()
    end
end
------------------------

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        newCmd(plr, msg, "kill", kill) -- just don't include the parameters...
    end)
end)

0
you need some return User#19524 175 — 6y
0
@laughablehaha But if I do that then I can't do functions that require different params tjtorin 172 — 6y
Ad

Answer this question