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

[Yes Or No Question] Is running a function from a argument possible?

Asked by
Vezious 310 Moderation Voter
8 years ago
local NPC = script.Parent.Tori


local function VanillaIcecream()
local Coordinates = script.Parent.Coordinates.VanillaIcecream.Value
NPC.Humanoid:MoveTo(CFrame.p)
NPC.Humanoid.MoveToFinished:wait()
NPC:SetPrimaryPartCFrame(CFrame)
end



function game.ReplicatedStorage.Events.SnackToServer.OnServerInvoke(Player,Type)
print(Type)
Type()
end

What I want to do is, you see that there is a function in the script. And then there is a event that is received from a client, with the argument Type. The Type name is called "VanillaIcecream, the same as the function. Is it possible to do Type()/VanillaIcecream()

Outputattempt to call local 'Type' (a string value)

0
Please tell us what you want to do with this script a little more lukeb50 631 — 8y
0
Now? Vezious 310 — 8y
0
Wait, I'm confused. Are you asking if you can pass a function as an argument to remotes? Perci1 4988 — 8y
0
Ok I updated it to my real script. Vezious 310 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

In Lua, functions are variables like any other. What' special about these variables is that they can be invoked by adding parentheses on to the end.

Therefore, they can be passed as arguments to other functions and then called.

Here's an example:

local x = 5

local function fancyPrint(arg)
    print(arg)
end

local function someFunction(func, number)
    func(number)
end

someFunction(fancyPrint, x)
Ad

Answer this question