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

Why does my NPC stop moving when using a repeat wait() loop?

Asked by 5 years ago

Hello, I've been working on my NPC for abit now and i've managed to find a few threads on pathfinding about using a repeat wait loop in order to make the npc to transition smoothly between each parts instead of being jittery.

Here is my code:

--/// Variables 
local Target
local PathfindingService = game:GetService("PathfindingService")

local NPC = script.Parent
    local Humanoid = NPC.Humanoid
    local RootPart = NPC.HumanoidRootPart


--Temporary!!!
game.Players.Changed:Connect(function()
    --/// Random plr Target
    if #game.Players:GetPlayers() >=2 then
        local rndTarget = workspace:FindFirstChild(game.Players:GetPlayers()[math.random(#game.Players:GetPlayers())])
        game.Players:GetPlayers()[rndTarget].CharacterAdded:wait()
        Target = rndTarget.Character:WaitForChild("HumanoidRootPart")
    else
        game.Players:GetPlayers()[1].CharacterAdded:wait()          
        Target = game.Players:GetPlayers()[1].Character:WaitForChild("HumanoidRootPart")
    end
    ----
end)
----



-- Main
while true do wait()
    local Start = RootPart.Position
    local Goal = Vector3.new(Target.Position.X -7,Target.Position.Y, Target.Position.Z)

    local Path = PathfindingService:FindPathAsync(Start, Goal) -- Makes a path from the starting Point (RootPoint) to the Goal (Target)

    if Path.Status == Enum.PathStatus.Success then -- Makes sure that the path is valid

        local Waypoints = Path:GetWaypoints() -- gathers all the points     
        for i, Waypoint in pairs(Waypoints) do -- for each point do
            Humanoid:MoveTo(Waypoint.Position) -- Moves to each individual point



            if Waypoint.Action == Enum.PathWaypointAction.Jump then -- Jumping Phase
                Humanoid.Jump = true                
            end         

            repeat wait()
                Distance = (Start - Waypoint.Position).magnitude
            until Distance < 3



        end 
    end
end

My NPC moves 2-3 studs then stops completely. And the script essentially breaks. Is that what the until part of it does?

0
The until is ment to stop the body once it is 3 studs away from its target. masterblokz 58 — 5y
0
How would i make it so it then moves onto the next loop smoothly then? NoirPhoenix 148 — 5y

Answer this question