How do i make a boolean global?

Im relatively new to scripting (less than a week) and im still not sure how to make my boolean global.
I added a double jump to my game and want to make my character have two different animations for each jump (one for the normal and then a backflip for the second), so i figured that i could use a boolean that i already have for my double jump script to make the whole animation script run while its true, but i had a problem there; I don´t know how to make my variable global.
Any help is more than appreciated. Thank you very much! Have a nice day.

All you have to do is remove the “local” keyword.

local bool = true --local scope
bool = true --global scope
1 Like

Not sure if this is what you mean, but to make something global you need a value object.
Heres some source code:

local value = Instance.new(“BoolValue”)
value.Name = “” – here is the name
value.Parent = workspace

Then once that is done, if you want to check it you would do:

if workspace.(Value name here).Value == true then
print(“True”)
elseif workspace.(Value name here).Value == false then
print(“False”)
end

That will make it a variable in the global scope of that script. The original post mentioned two separate scripts, which isn’t the kind of “global” a global variable refers to.


You can accomplish this in two ways. I’d personally recommend using BindableEvents to communicate when the boolean changes. When you’re changing it in one script, fire the event using Bindable:Fire(), and have the other script update it accordingly with Bindable.Event:Connect(some function)

The alternative is using a ModuleScript to store the boolean instead of having a reference to the boolean directly. Then you would just check within some table to update your boolean and access the value through the same table.

5 Likes

Or just simply add a BoolValue in the Workspace, and put it in ReplicatedStorage.

local yourboolean = false;

--Some clause
if true then
                 yourboolean = true
end

It will change the global yourboolean

You can reuse that variable however you want. Also when don’t puta local yourboolean = true on a scope if you want it global otherwise local.

This can simply be achieved by using _G to create the global variable which can then be referenced in another script. These variables can be created by doing ‘_G.VariableName = false’ then in another script you can get that variable with ‘_G.VariableName’

I hope you find this helpful!

_G is often considered code smell, which is why I personally wouldn’t recommend it. It’s hard to guarantee when something will be initialized at run-time, since it doesn’t have the event structure of bindables. It’s also difficult to find where it’s being set from, potentially causing confusing errors if you accidentally use the same key in two different spots for completely different kinds of values. A more structured solution like ModuleScripts might be more beneficial, since it easier allows you to control what can and can’t set stuff through metatables and/or getters and setters instead if direct access.

2 Likes

Can’t you just use repeat wait() until _G.variablename for all scripts that use the variable? I also don’t use _G too often but I’d imagine this is a good workaround.

Hello, I hope you dont mind me firing (heh) this reply and asking the following question four years later:

You talk about two ways to store the boolean. I also have found another way in people’s scripts that I’m trying to learn from. That is: storing the boolean in the character and removing (doing a loop through what’s in character) when the action is finished (other scripts can check the character with a for loop too). Now I’m wondering whether thats good practice. What are the cons? And why would you recommend BindableEvents over either modulescript or perhaps the option I’ve described.

I would really like to have one way to do things, thats the easiest and most secure.

Yes if you want to code like YandereDev