How to wait till a bollean value is true?

I am trying to have a script wait until a bollean value is set to true, now it should only take a few seconds for it to become true, I don’t really want to use a loop unless nesacery. I am currently trying it with .Changed but fear it would be called afterwards which could cause issues down the line. Is there a way to “disable” a .changed event when a certain requirement is met or would I have to do it a different way?

3 Likes

You can disconnect the event once it meets the requirement.

If it’s happening only once (not a while loop) you can use (boolvalue).Changed:Wait()

Unsure if the first time it would happen would have it as false, so probably would not work all the time

Will try this and mark as the solution if successful.

As Vong25 said you can disconnect the function, if you don’t know how or you aren’t really sure, here’s a simplified version without using disconnect :

local ValueChanged = false -- Indicates if the value has changed

**your object***.Changed:Connect(function()
     if ValueChanged == false then -- If ValueChanged is false then it continues forward
          if **your object**.Value == true then
                -- your code here
               ValueChanged = true -- Changing it to true so that it won't be able reach far from the first if statement
          end
     end
end

you could also use

repeat wait() until value.Value == true
-- your code here
6 Likes

Don’t use any solutions provided here that use loops. Completely unnecessary.

if not boolValue.Value then boolValue.Changed:Wait() end

Checks if BoolValue isn’t falsy. If it is, then it waits for the value to be changed. The only other option that will fire Changed is if it gets changed to true.

Using this method, you aren’t unnecessarily writing loops and your code is event-driven. Connecting and disconnecting a throwaway event, because of RBXScriptSignal.Wait, is also a pointless method and long winded compared to the above.

If you need to wait for something like this, always check with initial state then use Wait. The second reply was correct in how this should be done: use BoolValue.Changed:Wait().

10 Likes

I would definitely do what @AeternaeMemori suggested. You want the script to do something when something happens. This is exactly what events are for.

Checking with loops is pointless, too, as they undermine the reason why we have Event:Wait() in the first place.

See the part in this article where “polling” is mentioned. It demonstrates exactly what you are trying to do and the correct way to do it:

(Although here instead of using a bindable event you can just use the Changed event of the bool value.)

5 Likes