At the moment I’m working on making a following script for a zombie using pathfinding. Every time I run this script I get the same error, does anyone know how to fix this or maybe know a better way to make a follow script.
the error:
Workspace.ActiveZombies.Zombie.FollowScript:31: attempt to index nil with number
server script:
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local HumanoidRootPart = script.Parent:FindFirstChild("HumanoidRootPart")
local Humanoid = script.Parent:FindFirstChild("Humanoid")
while true do
local found
local last = math.huge
local waypoints
for _,player in pairs(game.Players:GetPlayers()) do
local distance = player:DistanceFromCharacter(HumanoidRootPart.Position)
if distance < last then
found = player
last = distance
end
end
print(found)
local success, errorMessage = pcall(function()
path:ComputeAsync(HumanoidRootPart.Position, game.Workspace[found]:FindFirstChild("HumanoidRootPart").Position)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
end
Humanoid:MoveTo(waypoints[2].Position)
if waypoints[2].Action == Enum.PathWaypointAction.Jump then
Humanoid.Jump = true
end
Humanoid.MoveToFinished:Wait()
end