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
|
  |
| Joined: 10 Jun 2009 |
| Total Posts: 12174 |
|
|
| 08 Sep 2012 07:31 PM |
| "hi" is not a valid function. |
|
|
| Report Abuse |
|
|
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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 08 Sep 2012 07:54 PM |
| Do you not know how to use tables?? |
|
|
| Report Abuse |
|
|
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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 08 Sep 2012 07:58 PM |
Functions = {} Functions.Print = function(a) print("hi") end
Functions["Print"]()
that prints hi |
|
|
| Report Abuse |
|
|
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 |
|
|
|
| 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
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|