How to add constant conditional checks in a while loop

Hello, I’m returning after a year break from scripting and I forgot some things, one thing in specific that I’m stuck on is adding constant checks for a change during a while loop. For example, I’m trying to make an infinite while loop but let’s say there’s a WaitForChild statement in that while loop, but the child is never found and the loop is stuck forever. Is there a way to detect if the loop is stuck for whatever reason after a certain amount of seconds and restart the loop from the top? Thanks in advance

WaitForChild has a timeOut parameter.

2 Likes

use pcall or renderstepped/heartbeat/stepped connections or like this:

local worked = false
local firstTime = true

while worked do
      if firstTime then
            delay(5, function()
                 if worked == true then
                      break;
                 end
           end)

          firstTime = false
      end

      local t = workspace:WaitForChild("Character")
      if t then
          worked = true
      end
end
1 Like

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