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

string to function calling?

I was wondering if their was a way to call a function from a string as it might simplify my code.

Example of what I have right now;

local gMode = "FFA"

function FFA()
end

function Elim()
end

if gMode == "FFA" then --these if thens feel eww
	FFA()
elseif gMode == "Elim" then
	Elim()
end

Example of what I am looking for;

local gMode = "FFA"

function FFA()
end

function Elim()
end

gMode() --call FFA's function

The second example obviously doesn't work, but their a way to do it?

2 answers

✓ Accepted answer

You would just put them in a table which can be indexed to get the function though you can use meta methods to do the same thing.

local tbl = {}

function tbl.FFA()
	print("FFA")
end

function tbl.Elim()
	print("Elim")
end

local gMode = "FFA"
tbl[gMode]() -- index and run the function

hope this helps.

2 comments

0

i thought he wanted to assign it to the function lol

0

This was exactly what I wanted. Thank you!

###You can't call a string. You could do something like this though:

local gMode 

function FFA()
end

function Elim()
end

gMode = FFA -- assigning it to the function 

gMode() --call FFA's function

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