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

Why Isn't the pathfinding script for a zombie not working?

So I made a script that makes a zombie follow the player, but also have pathfinding (Meaning It can find the player In mazes,behind walls,etc. but It appears to not follow the player:

local NPC = script.Parent
local PathfindingService = game:GetService('PathfindingService')
local Players = game:GetService("Players")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

local path = PathfindingService:CreatePath()

path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

if path.Status == Enum.PathStatus.Success then
    local Positions = path:GetWaypoints()

    for i,v in pairs(Positions) do
        local ball = Instance.new("Part", workspace)

        ball.Position = v.Position
        ball.Anchored = true
        ball.CanCollide = false
        ball.Size = Vector3.new(4,4,4)
        ball.Transparency = 0.5
        ball.Shape = Enum.PartType.Ball

        NPC.Humanoid:MoveTo(player.PrimaryPart.Position)
        NPC.Humanoid.MoveToFinished:Wait(2)
    end
end

1 answer

✓ Accepted answer

you dont use the player.primarypart.position on the move to. you have to move to the ball because that makes the pathfinding well work

local NPC = script.Parent
local PathfindingService = game:GetService('PathfindingService')
local Players = game:GetService("Players")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

local path = PathfindingService:CreatePath()

path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

if path.Status == Enum.PathStatus.Success then
    local Positions = path:GetWaypoints()

    for i,v in pairs(Positions) do
        local ball = Instance.new("Part", workspace)

        ball.Position = v.Position
        ball.Anchored = true
        ball.CanCollide = false
        ball.Size = Vector3.new(4,4,4)
        ball.Transparency = 0.5
        ball.Shape = Enum.PartType.Ball

        NPC.Humanoid:MoveTo(ball.Position)
        NPC.Humanoid.MoveToFinished:Wait(2)
    end
end

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