How to Detect Boolean without [while true/wait do]

Im using Boolean value in my game to almost do alot of coding, and i need to a way to detect boolean value that changed with script, i dont want to use while true/wait do, is there easier way to do it.

I mean while true works but not efficent way i was thinking like if i want to print something when value changed by a script , print(“Value Changed”) but because its while true do its , never stops if value is true and keep printing “Value Changed”

You can use the :GetPropertyChangedSignal(“Property”) event of an instance. This event fires anytime the given property inside the parentheses has changed.

In the case of a bool value, you’d have to do something along the lines of
Bool:GetPropertyChangedSignal(“Value”):Connect(function()end)
Bool needs to be an instance in your game, meaning somewhere in your project you have to have a BoolValue instance existing.

I’d also recommend using an if statement to check if Bool.Value is true or false. After checking that, execute the proper code.

4 Likes

you can use Value.Changed:Connect(function()
Basically what this function does it checks if any properties of a value has been changed or something

While this method is definitely a solution, I would still recommend GetPropertyChangedSignal over .Changed as GetPropertyChangedSignal only fires when the specified property in the parentheses is changed (see my previous reply for context).

The argument I could see for a BoolValue though is since it has like 3 properties or something, it doesn’t really matter which one you use for it since it won’t be firing that often, but it’s still good practice to use the correct event.

can we detect only when its becamed true and not false?

for Values its generally better to use .Changed(value), as GetPropertyChangedSignal("Property") is used for other things like parts with many properties
so you can do

BoolValue.Changed:Connect(function(value)
 if value then 
    --the bool changed and is now true
 else
    -- the bool changed and is now false
 end
end)

Yes, you can do this by using an if statement to check if the BoolValue’s .Value property is equal to true.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.