Why isn't my if statement detecting the bool value?

Local Script:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent 
local BoolValue = game.ReplicatedStorage.BoolValue.Value

RemoteEvent:FireServer()

if BoolValue == true then
	print("hi")
end

Server Script:

local RemoteEvent = game.ReplicatedStorage.RemoteEvent 

RemoteEvent.OnServerEvent:Connect(function()
	game.ReplicatedStorage.BoolValue.Value = true
end)

I don’t understand why it isn’t detecting the bool value. Can someone help me understand?

You’re only checking once if the value is true, if you want it to actually print you should get up a changed event to detect the change.

BoolValue:GetPropertyChangedSignal('Value'):Connect(function()
    if BoolValue.Value == true then
        print("hi")
    end
end)
4 Likes

I’d recommend using the .Changed event in this case over a PropertyChangedSignal.

BoolValue.Changed:Connect(function(newVal)
      if newVal then
            print("Hi")
      end
end)

Both have the same effect, but using .Changed is the canonical way to check when an IntVal,StringVal, ObjectVal, BoolVal, etc. are changed. The new value is also passed along when the event is fired, so you wouldn’t have to reference BoolValue directly to know what the new value is.

Also, as a side note,

local BoolValue = game.ReplicatedStorage.BoolValue.Value

This line is a common mistake I find among many new scripters. Assigning a variable directly the value of an value object is almost never what you want to do (assuming you want the value to be updated as the object’s value is updated).

You should be using the object and de-referencing .Value everytime you want to check/modify the value as so:

local BoolValue = game.ReplicatedStorage.BoolValue
BoolValue.Value = false -- This would be how you reference the bool value
2 Likes

I find using GetPropertyChangedSignal to be more efficient, but both .Changed and :GetPropertyChangedSignal do basically the same thing

1 Like

Efficient in what regard though? Internally, I’m sure they function the same way (or very similarly), but if there’s already an in-built connection for this exact purpose, I’d recommend using that over a GetPropertyChangedSignal. I do agree that either way works though, so I guess it should be left up to the scripter’s discretion what they choose.

Exactly the reason why I chose to use GetPropertyChangedSignal, really no difference but from my experience, .Changed can very rarely fail to fire so I use this one instead.

Not necessarily “more efficient” per se , but it’s what I prefer :)