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

The pathfinding script for my zombie Isn't working, why?

Asked by 2 years ago

So I recently learned how to use pathfinding service! anyway the problem Is where the NPC Isn't following the player

script:

local NPC = script.Parent
local PathfindingService = game:GetService('PathfindingService')
local Players = game:GetService('Players')
local Player = Players:GetPlayerFromCharacter()

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

        NPC.Humanoid:MoveTo(v.Position)
    end
end

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

I believe the issue is with line 4, in order to find the local player in a server script, use the "PlayerAdded" function.

Replace this with line 4:

local player

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

repeat wait() until player ~= nil

I'm sorry about the delay!

P.S: The 'GetPlayerFromCharacter' function is to find the player if you know the character. e.g:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")    then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)

        print(player) -- Prints the player's name
        print(player.Parent -- prints "Players"
    end
end)
0
Still doesn't work! imnotaguest1121 362 — 2y
Ad

Answer this question