Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

This Pathfinding AI crashes my game?

Asked by 8 years ago

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)


0
If you just use the suggested code in my answer to your last question, you have none of these problems. BlueTaslem 18071 — 8y

1 answer

Log in to vote
0
Answered by
xAtom_ik 574 Moderation Voter
8 years ago

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)


Ad

Answer this question