Yes, I know exactly what's happening. If nothing from the credentials from the if statement are found then it will crash the game because the wait is embedded inside that if statement. But if I add an else, it will delay the whole script and make the NPCs move in a choppy motion because it's waiting 1/10th of a second for every 600 values inside the folder it has to check. So my question is, how do I not make it crash, but have it have no delay on the else?
repeat wait() until workspace.WalkPositions:findFirstChild("WalkPos_1") local WalkPositions = workspace.WalkPositions:GetChildren() local human = script.Parent.Humanoid local Torso = script.Parent.Torso local positions = {} local manginute = 25 spawn(function() while true do local i = math.random(1,#WalkPositions) local v = WalkPositions[i] if (v.Value - Torso.Position).magnitude < manginute and not positions[i] then human:MoveTo(v.Value) human.MoveToFinished:wait() table.insert(positions, i, v.Name) table.remove(positions, i - 1) else end end end)
The classic while true do
crash.
It is probably that you need a wait in your while true do
repeat wait() until workspace.WalkPositions:findFirstChild("WalkPos_1") local WalkPositions = workspace.WalkPositions:GetChildren() local human = script.Parent.Humanoid local Torso = script.Parent.Torso local positions = {} local manginute = 25 spawn(function() while true do local i = math.random(1,#WalkPositions) local v = WalkPositions[i] if (v.Value - Torso.Position).magnitude < manginute and not positions[i] then human:MoveTo(v.Value) human.MoveToFinished:wait() table.insert(positions, i, v.Name) table.remove(positions, i - 1) else end wait(0.01) end end)