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

Pathfinding for hostile npc wont work?

Asked by 5 years ago

Hello, I'm making a action game and i'm making the AI for the enemies. However whenever i run the script I get this error:

Workspace.Skeleton.Control:3: attempt to index field 'LocalPlayer' (a nil value)

This is the script:

while wait() do
    local player = game.Players.LocalPlayer.Character
    local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.HumanoidRootPart.Position,player.HumanoidRootPart.Position, 200)

    if path.Status == Enum.PathStatus.Success then
        local pts = path:GetPointCoordinates()
        if #pts < 3 then
            script.Parent.Humanoid:MoveTo(player.HumanoidRootPart.Position)
        else
            script.Parent.Humanoid:MoveTo(pts[3])
        end
    else
        script.Parent.Humanoid:MoveTo(player.HumanoidRootPart.Position)
    end
end

Help would be much appreciated :)

1 answer

Log in to vote
0
Answered by 5 years ago

You are trying to find the local player in a normal script. LocalPlayer can only be accessed in a local script. Hope this helps.

0
Thanks so much!!! darkstar2121a 13 — 5y
Ad

Answer this question