Script that changes bool value inside tool not working

I have this script inside a tool that when you activate the tool it changes a BoolValue (located inide the tool) to true, it does print true but the ValueOBJ(NAMED “Swinging”) dosent Change in the client nor the server , how could I fix this?

Thanks!

local swinging = script.Parent.Swinging.Value

script.Parent.Activated:connect(function()
	swinging = true
	print(swinging)
	
end)


3 Likes

I got a question, exchange of using bool value? Why don’t you use a variable, so, if you want to make other scripts access the variables just do this:

_G["swinging"] = false -- _G is same as shared, every script can access it, using _G.

script.Parent.Activated:connect(function()
	swinging = true
	print(swinging)
	
end)
1 Like

Yes, I needed another script to access it, thanks

1 Like

Another way to solve this, right now in your code you define the variable ‘swinging’ to the value of the Swinging Boolean at that point. So the swinging variable has been turned into a Boolean value which is set to it the linked objects value. Instead you want to link the swinging variable to the BooleanValue object, then set it’s value later.

So in easier terms; change the first line into;
local swinging = script.Parent.Swinging

Then when you change the value of swinging to true, type;
swinging.Value = true

6 Likes

Instead of changing Swinging.Value, you are instead changing swinging (the variable itself).

Fix:

local swinging = script.Parent.Swinging

script.Parent.Activated:connect(function()
	swinging.Value = true
	print(swinging.Value)
end)

Please don’t use _G.

5 Likes

Please don’t use _G? I gived him an alternative, also, why he shouldn’t use _G or shared?

It’s messy. Every single script in the game will see this seemingly useless swinging variable in _G, that refers to some rando’s tool animation.

And only one instance of this tool will ever work. Multiple instances will have the same _G and conflict.

2 Likes

It’s not messy, and not if you use local script on _G, can’t be accessed for other players

1 Like

What local script would set a bool value?

Look, you could refute my argument in any number of ways by attacking every reason I give for it, but at the end of the day you should (almost) never set things under _G.

And this is not a case that you should make an exception for. Ask any other experienced scripter why you should not use _G over BoolValues or local variables + module scripts.

2 Likes

Actually it would have. Not declaring a variable local and then referencing it or assigning to it actually sets it under _G.

local function your functions, people.

3 Likes