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

Pathfinding npc not moving at all?

Asked by 2 years ago

Full script:

local d = false
local maxDistance = 250
local PathfindingService = game:GetService("PathfindingService")
while wait() do
    local nearestCharacter, nearestDistance
    for i, v in pairs(workspace:GetChildren()) do
        if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") and v ~= script.Parent and v.Name ~= script.Parent.Name and v.Humanoid.Health > 0 then
            character = v
            local distance = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude
            if not character or distance > maxDistance or (nearestDistance and distance >= nearestDistance)
            then
                continue
            end
            nearestDistance = distance
            nearestCharacter = v
            local path = PathfindingService:CreatePath()
            path:ComputeAsync(script.Parent.PrimaryPart.Position, nearestCharacter.PrimaryPart.Position)
            local waypoints = path:GetWaypoints()
                for i, waypoint in pairs(waypoints) do
                    if waypoint.Action == Enum.PathWaypointAction.Jump then
                        script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                    end
                    script.Parent.Humanoid:MoveTo(waypoint.Position)
                    script.Parent.Humanoid.MoveToFinished:Wait(1)
                    if (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 6 then
                        if not d then
                            d = true
                            nearestCharacter.Humanoid:TakeDamage(10)
                            script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
                            wait(1.5)
                            d = false
                        end
                end
            end
            if (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 6 then
                if not d then
                d = true
                nearestCharacter.Humanoid:TakeDamage(10)
                script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
                    wait(1.5)
                    d = false   
                end

            end 
        end
    end
    if script.Parent.Humanoid.Health == 0 then
        break
    end
end
script.Parent.FollowAndAttack:Destroy()

Where I think the problem is:

local path = PathfindingService:CreatePath()
            path:ComputeAsync(script.Parent.PrimaryPart.Position, nearestCharacter.PrimaryPart.Position)
            local waypoints = path:GetWaypoints()
                for i, waypoint in pairs(waypoints) do
                    if waypoint.Action == Enum.PathWaypointAction.Jump then
                        script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
                    end
                    script.Parent.Humanoid:MoveTo(waypoint.Position)
                    script.Parent.Humanoid.MoveToFinished:Wait(1)
                    if (script.Parent.HumanoidRootPart.Position - nearestCharacter.HumanoidRootPart.Position).magnitude < 6 then
                        if not d then
                            d = true
                            nearestCharacter.Humanoid:TakeDamage(10)
                            script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
                            wait(1.5)
                            d = false
                        end
                end
            end

Tried putting that code in a loop but it didn't work, please help me.

Answer this question