Is there a better way to check a boolean value everytime on a script than "while wait() do"

Hello, to check if a boolean value is true or not, i always use while wait() do.
If boolean value is true, then stop the script, else, continue.
I always use this method and I feel like its not well optimized and there is a better way to do it.

1 Like

Yes, there are other ways to check a boolean value in a script. For example, you could use a for loop with a numeric range to check the value, as shown here:

for i = 1, 10 do
  if myBooleanValue then
    -- Code to run if myBooleanValue is true
    break
  end
end

Another option would be to use a repeat-until loop, which will continue to loop until the specified condition is met. Here is an example of using a repeat-until loop to check a boolean value:

repeat
  -- Code to run in the loop
until myBooleanValue

Both of these methods can be used as an alternative to using a “while wait() do” loop to check a boolean value in a script. Which method is best to use will depend on your specific needs and the rest of your code.

while wait() do is a bad practice and while true do should be used together with wait() instead. Regarding your question, if your intention is to block script execution until your boolean turns true you can do something like this:

while not (Boolean) do wait(0.1) end

The wait(0.1) is completely optional in this case and only acts as a way to preserve script performance.

Instead of indefinite loops to check if a value is true, just use a Changed event

BoolValue.Changed:Connect(function()
   if BoolValue.Value then
      --do something
   end
end)
1 Like