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

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

Asked by 5 years ago

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

0
load string but you shouldn't have it on User#19524 175 — 5y

1 answer

Log in to vote
1
Answered by
thebayou 441 Moderation Voter
5 years ago

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)
Ad

Answer this question