Changing bool value doesn't work

What I am doing is simple:

  1. Player inputs command
  2. Server receives action through RemoteEvent
  3. Server checks if two bool values are disabled
  4. Server enables one value

But it doesn’t :confused:


The script receives the command, I have checked with prints and other stuff it should do. BUT it just doesn’t activate the value. Script:

local RemotesFolder = game.ReplicatedStorage:WaitForChild("ComputerEvents")
local Tower2 = workspace.Tower2
local Activated = Tower2.Activated.Value
local UserActivated = Tower2.UserActivated.Value
RemotesFolder.TowerManage.OnServerEvent:Connect(function(player,WhoActivated)
    if WhoActivated == "user" then
		if not UserActivated and not Activated then
			print("1")
			UserActivated = true
			print("2")
			workspace.Interface.Interface.ClickDetector.MaxActivationDistance = 5
			activate:Play()
			for i, v in pairs(Tower2:GetChildren()) do
				if v.Name == "Neon" then
					v.BrickColor = BrickColor.new("Royal purple")
				end
			end
			wait(activate.Length)
			idle:Play()
		elseif UserActivated and not Activated then
			Error()
		elseif not UserActivated and Activated then
			Error()
		end
	elseif WhoActivated == "dev" then
		
	end
end)

1 and 2 are getting printed. What can I do?

UserActivated is a local variable, so changing it, just changes that variable
To change the actual value you need to use

Tower2.UserActivated.Value = true

2 Likes

But I have specified above that local UserActivated = Tower2.UserActivated.Value. How it is a local variable?

1 Like

Because that local variable is just a copy of that true/false value. When you change it you just change the value of your local UserActivated variable. It won’t change the value of TowerTwo.UserActivated.Value

To change the value of that you need to use
TowerTwo.UserActivated.Value = true

Can I make it shorter by typing UserActivated.Value = true ?

BTW it worked


Edit: with all of this in mind I will go the old classic way, if UserActivated.Value == false then. Thanks.

2 Likes

I think the code is about as short as it can be, looks good to me. You can’t set UserActivated.Value because UserActivated is your local true/false variable, it has no value, so it must stay as Tower2.UserActivated.Value = true

Unless, as I think you just said if you change it to be
local UserActivated = Tower2.UserActivated
Then you can say
UserActivated.Value = true
but everywhere else you change to say If UserActivated.Value == false.

That works too

The only thing I’d suggest, IF you have lots of towers (I’m only guessing this because the tower here is called Tower2, so there might be Tower1, Tower3, etc. etc.) would be to do something so you don’t repeat this code for each tower, but with different Tower1,Tower2,Tower3 put in.

2 Likes