Wait x second or until a condition becomes true?

could someone provide an example of how this can be done?

1 Like

the way to do this is use repeat, you can repeat waiting until the condition is true and then move on, I don’t know if this is the best way to do this, but it’s like this

local bool = false

coroutine.wrap(function() -- This is just so it doesn't interfere with the repeat
    wait(3)
    bool = true
end)()

repeat wait() until bool -- this will stop the script until the bool is true, after three seconds, it will continue
print("Started")

also next time. please have a script

2 Likes

yes but how do I make it so if the condition is not met within 1 second it doesn’t print “started” for example

Do you mean something like this, for example?

local Condition = false
local SecondsToWait = 1
local Count = 0

repeat
      wait(SecondsToWait)
      if not Condition and Count < 3 then
         Count = Count + 1
         if Count >= 3 then
            Condition = true
         end
      else
          if Condition and Count > 0 and Count < 2 then
             print("Started")
          end
      end   
until Flag
1 Like

This is where Repeat comes.
So Repeat is used for repeating a specific thing or function until a condition has met.
For example if a condition has to be true

Here is an example

local part = game.Workspace.Part -- so this is a part in workspace and the part is unanchored.

repeat wait() -- we are repeating a wait no matter what time is inside the wait it would be infinite or will continue until the part is anchored
until part.Anchored = true 
print("This is a Repeat Loop") -- so when you run the game this `Print` would only print when the part is unanchored

You can learn more in this video -

or reading this API Reference about loops

You can try using this:

local Bool = false
local TimeToWait = 3 -- Maximum amount of time until code ends
local WaitTime = TimeToWait/5 -- Amount of time to wait in between 
local Iterations = 0 -- Something to check if time has been met or not

while not Bool and not Iterations >= TimeToWait + 1 do -- Repeats this loop until 3 seconds is met or Bool becomes true
    wait(WaitTime) -- Waits a specified amount of time
    Iterations = Iterations  + WaitTime -- Increases the iterations
end

if Bool then -- Checks if Bool is true
    -- Code
else -- If not true then something will happen
    -- Code
end

Basically how this code works is that it either waits 3 seconds, or waits until the Bool is true, whichever comes first.

My Solution:

local result
local condition = false
local limit = 5
coroutine.wrap(function()
local i = 0
repeat wait(.001)  i = i + .001 until result or i >= limit
if result then condition = true end
end)()
wait(limit)
if condition then
--code
end

That is not how this works, you’re attempting to set the part’s Anchored property when you should check if it’s true.

Also, there isn’t even a need for a repeat loop as you can do this instead.

local Workspace = game:GetService('Workspace')
local Part = Workspace.Part

Part:GetPropertyChangedSignal('Anchored'):Connect(function()
	print(('%s is now %s'):format(Part.Name, Part.Anchored and 'anchored' or 'unanchored'))
end)

Also:

A repeat loop checks the condition at the end of each iteration while while loop checks it at the beginning of each iteration.
So repeat loop executes at least one iteration while while loop may not execute any iterations if the condition is not fulfilled. That’s the difference.

1 Like