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: Issues with global variables

Previous Thread :: Next Thread 
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
24 Jul 2013 01:23 PM
For a custom tycoon system I'm making, I open a GUI and set an object value to the script that opens the gui in the tycoon button. I'm trying to use a script in the GUI to call a global function in the script that opened the GUI, but I get an error. Here's my code:

gui.CurrentBuy.Value = script;

_G[script].doUpgrade = function()
machine = factory.Stuff.Model1.Value:Clone()
machine.Parent = factory
machine:MakeJoints()
script.Parent.Parent:remove()
end

and I'm calling it with
_G[current.Value].doUpgrade()

(current is the CurrentBuy value)

Error is Player1.PlayerGui.Main.Clicked:21: attempt to index field '?' (a nil value)
Why?
Report Abuse
Tuxwonder is not online. Tuxwonder
Joined: 21 Jan 2008
Total Posts: 888
24 Jul 2013 01:35 PM
Just to make it more clear... which line is line 21?
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
24 Jul 2013 05:03 PM
_G[current.Value].doUpgrade()
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
24 Jul 2013 07:26 PM
Please help.
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 09:23 AM
Anyone?
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jul 2013 09:25 AM
Not enough information.

Are both of the scripts, normal scripts, local, or 1 normal 1 local?
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 09:33 AM
Both normal.

BuyGui:

factory = script.Parent.Parent.Parent
ting = 0

function onTouched(hit)
print(ting)
if ting == 0 then
ting = 1
local owner = factory.OwnerName.Value
check = hit.Parent:FindFirstChild("Humanoid")
if check ~= nil then
local user = game.Players:GetPlayerFromCharacter(hit.Parent)
if owner == user.Name then
local gui = user:findFirstChild("PlayerGui"):findFirstChild("Main")
gui.Cost.Value = script.Parent.Cost.Value;
gui.Repair.Value = script.Parent.Repair.Value;
gui.Help.Purchase.Text = "Purchase ($" .. gui.Cost.Value .. ")";
gui.Help.UpgradeTitle.Text = "t";
gui.Help.UpgradeDesc.Text = "tt";
gui.CurrentBuy.Value = script;
gui.Help.Visible = true;
end
end
ting = 0
end
end

script.Parent.Touched:connect(onTouched)

_G[script].doUpgrade = function()
machine = factory.Stuff.Model1.Value:Clone()
machine.Parent = factory
machine:MakeJoints()
script.Parent.Parent:remove()
end



Clicked


scr = script.Parent;
help = scr.Help;
closebtn = help.Close;
buybtn = help.Purchase;
repbtn = help.Repair;
current = scr.CurrentBuy;
cost = scr.Cost;

closebtn.MouseButton1Click:connect(function()
help.Visible = false;
end);

buybtn.MouseButton1Click:connect(function()
print("one")
local stats = scr.Parent.Parent:findFirstChild("leaderstats")
if stats ~= nil then
print("two")
local cash = stats:findFirstChild("Money")
if cash.Value > (cost.Value - 1) then
cash.Value = cash.Value - cost.Value;
_G[current.Value].doUpgrade()
end
end
end);
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 09:53 AM
Does the script not transfer right into the ObjectValue? I use the script itself as the index for the function, would the function of an objectvalue be the same type of object?
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 10:34 AM
Please!
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jul 2013 10:35 AM
Add a wait to the top so it gives "time" for the_G's to load
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 10:42 AM
I use the gui button ~30 seconds after the world loads, it should work, shouldn't it?
Report Abuse
Eversome is not online. Eversome
Joined: 07 Oct 2012
Total Posts: 32
25 Jul 2013 10:44 AM
I don't believe you can use objects as indices.
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 10:48 AM
The wiki says otherwise: http://wiki.roblox.com/index.php/Reusing_code#Organization_is_key
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 11:10 AM
Please help!
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 11:49 AM
Anyone?
Report Abuse
Tuxwonder is not online. Tuxwonder
Joined: 21 Jan 2008
Total Posts: 888
25 Jul 2013 11:53 AM
The _G table has always been incredibly iffy for me. I try to use local scripts with local things for the _G table. If there aren't that many things you're trying to pass between the scripts, like maybe a couple numbers, strings, or objects, you should just use a Value object and place it somewhere the scripts can get to it and edit it.

Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
25 Jul 2013 12:12 PM
Stunt,

Smart of u to put the link, 'cause I had no idea what u were talking about:

_G[script].doUpgrade = function()


machine = factory.Stuff.Model1.Value:Clone()
machine.Parent = factory
machine:MakeJoints()
script.Parent.Parent:remove()
end



Clicked


scr = script.Parent;
help = scr.Help;
closebtn = help.Close;
buybtn = help.Purchase;
repbtn = help.Repair;
current = scr.CurrentBuy; -- This is a tag to an Object in the Parent of "Clicked" scipt name o
cost = scr.Cost;

closebtn.MouseButton1Click:connect(function()
help.Visible = false;
end);

buybtn.MouseButton1Click:connect(function()
print("one")
local stats = scr.Parent.Parent:findFirstChild("leaderstats")
if stats ~= nil then
print("two")
local cash = stats:findFirstChild("Money")
if cash.Value > (cost.Value - 1) then
cash.Value = cash.Value - cost.Value;


print
(current, current.Value)
_G[current.Value].doUpgrade() -- current here, had better be an ObjectValue set to the name of the Global script. Is it? You have never mentioned setting the .Value of current. Don't use a variable here, just put the path to the local script.

'Cause this is what u want:

_G[game.Workspace.GlobalScript].GlobalFunction() -- [Path-to-Global-script].GlobalFunctionName()

U can put print statements all over the place to actually SEE what is going on, instead of guessing; asking; and guessing & askiing..... Ask the comp, what u r doing.

GL,
Brian


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jul 2013 12:15 PM
^^ not going to work
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
25 Jul 2013 12:21 PM
* Don't use a variable here, just put the path to the local *(Global) script.
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
25 Jul 2013 12:25 PM
if u insist on indirect accessing here then u had better set the .Value of current to the path to your global script:

print (current, current.Value)
current.Value = Where-ever-global-script-is
print ("After settin value of current", current, current.Value)
_G[current.Value].doUpgrade()
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
25 Jul 2013 12:27 PM
^ That's not how Global Variables work...
Report Abuse
BJCarpenter is not online. BJCarpenter
Joined: 04 Nov 2008
Total Posts: 4416
25 Jul 2013 12:31 PM
Can't,

I told him I didn't know what he was talking about, but the print statements are worth their weight in Gold, and will point him in the right direction.....
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 12:50 PM
Well, can someone help? Why won't my script work? Also, putting the path won't work, there are multiple models with the same name that these scripts are in.
Report Abuse
haloguy443 is not online. haloguy443
Joined: 16 Apr 2012
Total Posts: 253
25 Jul 2013 12:59 PM
CurrentBuy Is not a function, It's a Nil value.
Report Abuse
stuntrally8 is not online. stuntrally8
Joined: 20 May 2008
Total Posts: 2090
25 Jul 2013 01:18 PM
That's why I use CurrentBuy.Value?
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