|
| 13 Jul 2011 07:41 PM |
| How do you call a global function via string? |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 07:46 PM |
| The string would need to be the function name. _G[string] G string. hahaha. |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 13 Jul 2011 07:50 PM |
Then why won't this work?
Character = script.Parent
Character.Primary.Changed:connect(function(type)
if _G[type]() ~= nil then
if pcall (function() _G[type]() end) then else return end
end
end) |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Jul 2011 07:56 PM |
Right now I'm testing it...
function _G.Rifle() print("Rifle") end
wait(2) game.Workspace.Part.Primary.Value = "Rifle" |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 07:56 PM |
Because of this:
if _G[type]()
You only include parantheses when you are calling your function, not checking if the index is there. |
|
|
| Report Abuse |
|
|
| |
|
|
| 13 Jul 2011 07:59 PM |
Oh, you're indexing your global function wrong. Like this:
_G.Rifle = function() print("Rifle")
_G.Rifle() |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:00 PM |
| Oh, put an end in there. I Forgot it. |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:01 PM |
| But what if I'm gonna put more lines of code? |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:02 PM |
_G.Rifle = function() print("line 1") print("\nline2") print("\nline3") end
Lua doesn't care about whitespace. |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:02 PM |
Like so:
Script 1:
_G.String = function() print("Hello world!") --any other lines of code end |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:02 PM |
| Oy, I found the problem. The 'Changed' thing won't fire up. |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:03 PM |
| .Changed has had some issues, it's a weird event. |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:16 PM |
I did this :
Script 1
_G.Rifle = function() print("Rifle") end
wait(5) game.Workspace.Part.Primary.Value = "Rifle"
Script 2
Character = script.Parent
--Character.Primary.Changed:connect(function(type)
if pcall (function() _G[type]() end) then else print(error) end
--end)
And it printed : function: 1F75A160 |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:17 PM |
| .Changed isn't firing for me... |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:17 PM |
Because you need to pcall like this:
if pcall(_G[type]()) then else print(error) |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:23 PM |
| Workspace.Part.Script:5: bad argument #1 to 'pcall' (value expected) |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:23 PM |
Here's the script :
Character = script.Parent type = Character.Primary.Value --Character.Primary.Changed:connect(function(type)
if pcall(_G[type]()) then else print(error) end
--end) |
|
|
| Report Abuse |
|
|
|
| 13 Jul 2011 08:25 PM |
| Oh, right, remove the parantheses. Just use pcall(_G[type]) |
|
|
| Report Abuse |
|
|
| |
|