Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

2 comments

0

you need some return

0

@laughablehaha But if I do that then I can't do functions that require different params

This discussion is preserved as it was. New answers, comments, and votes are closed.