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: _G Help

Previous Thread :: Next Thread 
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 07:41 PM
How do you call a global function via string?
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 07:46 PM
The string would need to be the function name. _G[string] G string. hahaha.
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 07:47 PM
Don't you need the '()'?
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 07:48 PM
_G[string]()
Yep.
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
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
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
13 Jul 2011 07:53 PM
Let's see your functions.
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
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
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
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
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 07:58 PM
Doesn't print anything...
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
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
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 08:00 PM
Oh, put an end in there. I Forgot it.
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 08:01 PM
But what if I'm gonna put more lines of code?
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
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
socccerstar is not online. socccerstar
Joined: 23 Aug 2008
Total Posts: 489
13 Jul 2011 08:02 PM
Like so:

Script 1:

_G.String = function()
print("Hello world!")
--any other lines of code
end
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 08:02 PM
Oy, I found the problem. The 'Changed' thing won't fire up.
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 08:03 PM
.Changed has had some issues, it's a weird event.
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
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
nickmaster24 is not online. nickmaster24
Joined: 04 Oct 2008
Total Posts: 8906
13 Jul 2011 08:17 PM
.Changed isn't firing for me...
Report Abuse
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 08:17 PM
Because you need to pcall like this:

if pcall(_G[type]()) then else print(error)
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 08:23 PM
Workspace.Part.Script:5: bad argument #1 to 'pcall' (value expected)
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
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
DingDong272 is not online. DingDong272
Top 100 Poster
Joined: 08 Dec 2007
Total Posts: 9860
13 Jul 2011 08:25 PM
Oh, right, remove the parantheses. Just use pcall(_G[type])
Report Abuse
deathman12e3 is not online. deathman12e3
Joined: 10 Nov 2009
Total Posts: 7284
13 Jul 2011 08:51 PM
Woot! Thanks DingDong.
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