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

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

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)

4 comments

0

Please tell us what you want to do with this script a little more

0

Now?

0

Wait, I'm confused. Are you asking if you can pass a function as an argument to remotes?

0

Ok I updated it to my real script.

1 answer

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)

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