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

how to call a function inside a table by a string?

so i kind of want my code to look like this..

local function something() -- make sure to put this inside a pcall() function
			local item 
			for i,v in pairs(slot:GetChildren()) do  -- gets item inside selected
				
				if v:IsA("Model") then
					
					item = v
				end
			end
			func(item.Name)(item.Animation,slot.Name) -- item.Name == the func name

			
		end
		something()
	end

so basicly my idea is to put hole bunch of functions inside a script and when a player wants to use a item. it calls the function by the name of the item. for example if somone used a apple the script would find whats inside the slot (apple) and then call the function with its name and the arguments. let me know if this is kind of hard to understand and ill try to explain it better. thanks

1 comment

0

load string but you shouldn't have it on

1 answer

✓ Accepted answer

Functions, like any other variables, can be stored in tables.

local funcs = {oneFunc, twoFunc, blueFunc, redFunc}

You can then access the functions stored in the table just like any other item as well:

funcs["oneFunc"]

You can also call it:

funcs["oneFunc"](arguments)

Another example:

local functionName = "redFunc"
funcs[functionName](arguments)

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