generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Calling A Function From A String

Previous Thread :: Next Thread 
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:30 PM
I'm trying to call a function from a variable.

If i have a function called hi and a variable with a string called Func,
how would I make the string convert? into a function?

local Func = "hi"


I've tried:

pcall(Func)
loadstring(Func)
load(Func)()
Func()

Nothing is working, any help would be appreciated.
Report Abuse
TaslemGuy is not online. TaslemGuy
Joined: 10 Jun 2009
Total Posts: 12174
08 Sep 2012 07:31 PM
"hi" is not a valid function.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:32 PM
Lol if i made a function called hi()
Report Abuse
TaslemGuy is not online. TaslemGuy
Joined: 10 Jun 2009
Total Posts: 12174
08 Sep 2012 07:33 PM
Oh, I see.

What you should do is make a table of the relavant functions, and then use the table to access them.


local funcs = {}
funcs.hi = ...


funcs["hi"]()

or

local f = "hi"
funcs[f]()
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:34 PM
Example:

function Hello()
print("Hello")
end

local Variable = "Hello"
--How would i call the function from a string
pcall(Variable) --Doesn't work
loadstring(Variable) -- Doesn't work
Report Abuse
TaslemGuy is not online. TaslemGuy
Joined: 10 Jun 2009
Total Posts: 12174
08 Sep 2012 07:37 PM
I just told you.

Make a table of the functions you want, then reference the table.
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:39 PM
Like is there a 'string to function' or something of the sort?
The textbutton and the function are the same name
Here is the script I'm using

local Names = {"Kill", "Kick", "Heal", "Freeze", "UnFreeze"}

for i = 1, 5 do
..if #Names == 0 then return end
..script.Parent[Names[i]].MouseButton1Down:connect(function()
--Call Function Names[i]
..end)
end
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:39 PM
I don't get how i would do the table, it's a bit confusing
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:53 PM
bump
Report Abuse
XxChopSlaperxX is not online. XxChopSlaperxX
Joined: 09 Nov 2010
Total Posts: 3112
08 Sep 2012 07:54 PM
Do you not know how to use tables??
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:54 PM
I know how to use tables but it won't work with functions the way i'm doing it

do you not understand what i'm asking?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 07:56 PM
I have a gui with textbuttons called: Kill, Kick, Heal, Freeze, UnFreeze
Here is the script

local Names = {"Kill", "Kick", "Heal", "Freeze", "UnFreeze"}

function Kill()
print("Kill")
end

function Kick()
Print("Kick")
end

function Heal()
print("Heal")
end

function Freeze()
print("Freeze")
end

function UnFreeze()
print("UnFreeze")
end

for i = 1, 5 do
...if #Names == 0 then return end
...script.Parent[Names[i]].MouseButton1Down:connect(function()
......--Call Names[i]
...end)
end
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
08 Sep 2012 07:57 PM
it would be much simpler to make a table of all of the functions, not their names.

tab = {["kill"] = function() end; ["stuff"] = function() end;}

for i = 1, #tab do tab[i]() end

i guess you could do it like this

local Names = {"Kill", "Kick", "Heal", "Freeze", "UnFreeze"}

for _, v in pairs(Names) do
getfenv()[v]
end

¬ Scripter Tier-2, LuaLearners Elite ♣ scripting teacher/freelance worker ♣ send me trade requests!
Report Abuse
XxChopSlaperxX is not online. XxChopSlaperxX
Joined: 09 Nov 2010
Total Posts: 3112
08 Sep 2012 07:58 PM
Functions = {}
Functions.Print =
function(a)
print("hi")
end

Functions["Print"]()

that prints hi
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 08:01 PM
for _, v in pairs(Names) do
script.Parent[v].MouseButton1Down:connect(function()
getfenv()[v]
end)
end

Output, Missing '='

Last time, tables won't work...
Report Abuse
doombringer42 is not online. doombringer42
Joined: 13 Nov 2007
Total Posts: 5445
08 Sep 2012 08:03 PM
try this:

for _, v in pairs(Names) do
script.Parent[v].MouseButton1Down:connect(function()
getfenv()[v]()
end)
end

and i don't know why you don't want to use a table of functions. it would work just as well.

¬ Scripter Tier-2, LuaLearners Elite ♣ scripting teacher/freelance worker ♣ send me trade requests!
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
08 Sep 2012 08:05 PM
=D It Woked
Tyvm
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image