Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do I Fix Global Variables?

I made a code:

function onPlayerJoin(player)
	leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
        
        stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Value = 1
	stage.Parent = leaderstats
 end
        
        game.Players.PlayerAdded:Connect(onPlayerJoin)

It says

Global stage is only used in the enclosing function; consider changing it to local?

Global leaderstats is only used in the enclosing function; consider changing it to local?

But it is in this code as well (Different script in different object)

function stageTwo()
	stage.Value = 2
end

script.Parent.Touched:connect(function(stageTwo)
	stage.Value = 2
end)

So why is that error there and why does it say not defined stage and in the second code?

More importantly, how do I fix it?

2 answers

✓ Accepted answer

When a function is global, it doesn't mean it will be accessible from other scripts. It means that it will be accessible outside of the local scope of that particular chunk of code, within the same script. Rarely will a time arise where you need a variable to be global, which is why it's always best to keep them local. A local variable will take less memory, and is easier to keep track of to avoid causing a memory leak.

If you're looking for a way to share values between scripts, consider using a ModuleScript, or even BindableEvents if you only need to share the values when certain things happen.

2 comments

0

I think I need it to be global because I am making an obby and I want to make it so when you hit a checkpoint, it changes the value stage to whatever stage they are on. How do I do this?

0

What might be helpful in your case is to use an IntValue (Look it up on the developer hub) so that other scripts can take a look at its value to find what stage the player is on.

Answer to #1: listen to the output lol. I don't know if this will fix all your problems, but this should help. In LUA, the first time you define a variable, it needs to have local in front of it, unless you want the entire script to be able to read/write to it. Since the variable is only used in one function, the error output is suggesting to change it to local, which you probably should do.

Answer to #2: A global variable is only global to the script it is in. You'll need to manually get the stat from the player to adjust it in future scripts. You can do that like this:

local Stage = player:WaitForChild("leaderstats"):WaitForChild("Stage")

Stage is the intvalue that you made. I hope this helps. If you have concerns about this question/answer dm me Blobmaster#8156 or message me on roblox. only message me if you have a concern regarding this topic pls. Till next time.

This discussion is preserved as it was. New answers, comments, and votes are closed.