Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This Pathfinding AI crashes my game?

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)


1 comment

0

If you just use the suggested code in my answer to your last question, you have none of these problems.

1 answer

✓ Accepted answer

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)


This discussion is preserved as it was. New answers, comments, and votes are closed.