#Convert string to Function / File name

1 messages · Page 1 of 1 (latest)

frosty grotto
#

so i have a string accesible with :GetAttribute(), i would like to fire a function with the same exact name as :GetAttribute() returns but i do not know if its possible. Example :

UIS.InputBegan:Connect(function(input, processed)
local String = humanoid:GetAttribute("CurrentFruit") -- fire a function setup earlier with the string that this line will return OR access an object name with the returned

ReplicatedStorage.String
end```
idk if what im trying to get is clear but i tried my best 😭
mystic wyvern
#

i'd just use if statements if i were you

late zephyr
#

Just adjust more according to your needs

frosty grotto
#

im guessing the ["Apple"] = dummy is creating the attribute?

late zephyr
#

Wait, I might interpret your question differently.

#

Are you going to access ReplicatedStorage?

#

With the key "String"?

#

To make sure we are in the same boat, do you want to call the function named value in variable "String"?

frosty grotto
#

i think we're not understanding each other xD lets restart more clearly
i have an attribute on the players humanoid called "Power" holding a string, lets make it hold "ArchAngel" for now
in a local script, i have a funciton of the same name, "ArchAngel", but i also have alot of other functions for each power basicly.
i'd like to call the function of the same name held by the humanoid attribute without having an if statement for each

late zephyr
#

Ah so my understanding was correct

#

Use tables

#

It should be something like this

function dummy1()
    print("Power function")
end

function dummy2()
    print("ArchAngel function")
end

local myfunc = {
    ["Power"] = dummy1,
    ["ArchAngel"] = dummy2
}

myfunc[humanoid:GetAttribute("CurrentFruit")]()
wooden forumBOT
#

studio** You are now Level 1! **studio

late zephyr
#

You can extend myfunc table by adding more keys that corresponds to which function

#

Or if you want more cleaner version, I'd suggest using module script

frosty grotto
#

can you detail what each part of the script does im having trouble understanding?

#

i've never seen brackets being used while calling a fcuntion

frank sequoia
#

theyre basically finding the function inside a table

#

so

#

if table[1] was a function u wanted to cslk

#

call

#

youd do

#

table1

frosty grotto
#

so like myfunc holds all the functions, and what i put in the [ ] is the string associated to the function

#

which i get from the :GetAttribute() ?

frank sequoia
#

yes

frosty grotto
#

this is awesome ty woe

frank sequoia
#

its the index of the table

frosty grotto
#

also i think i might've made a mistake somewhere since when i startup the game both ult functions fire at the same time 😭

function MadStrength()
--[...]
end 
function DemonstrationOfPower()
--[...]
end 
local UltFunctions = {
    ["Strength"] = MadStrength(),
    ["ArchAngel"] = DemonstrationOfPower()
}

--Ult command
UIS.InputBegan:Connect(function(input, processed)
    local humanoid = LocalPlayer.Character.Humanoid
    if input.KeyCode == Enum.KeyCode.G then
        if Gauge.Value >= 100 and humanoid:GetAttribute("CanCast") == true then
            UltFunctions[humanoid:GetAttribute("Power")]()
        end
    end
end)```
frank sequoia
#

no no

#

dont call it

#

so when u asigjnit here

frank sequoia
#

dont use parenthesis

#

functions are saved in names of variable

#

() calls the callback

frosty grotto
#

Of okay ill try it im eating rn

#

Ty ry