So pathfinding has always been a no go for since I don't tend to rely on AI for anything but I'm making a zombies mode for my game and I found this script and I tried to edit it as best as I can but nothing is working. With multiple players the zombie will lock on to a specific player that is closest to its spawning location and if that player dies the zombie will just stand there and not go to any other players.
repeat wait() until script.Parent:FindFirstChild("HumanoidRootPart")
local players = game.Players:GetPlayers()
local player = players[1]
local i = 1
for i=1,(#players) do
if player.Character.Down.Value == false then
local playerNew = players[i]
local Prevmagnitude = (player.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).magnitude
local Magnitude = (playerNew.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).magnitude
if Magnitude < Prevmagnitude then
player = players[i]
end
i = i + 1
end
end
while wait() do
local path = game:GetService("PathfindingService"):ComputeRawPathAsync(script.Parent.HumanoidRootPart.Position,player.Character.HumanoidRootPart.Position,512)
if path.Status == Enum.PathStatus.Success then
local points = path:GetPointCoordinates()
if #points < 3 then
if player.Character.HealthV.Value > 0 then
script.Parent.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position)
end
else
if player.Character.HealthV.Value > 0 then
script.Parent.Humanoid:MoveTo(points[3])
end
end
else
if player.Character.HealthV.Value > 0 then
script.Parent.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position)
end
end
end
Any suggestions on how to fix this?

1 comment
It does what you program it to do, so in the code provided, you show that it follows only the first person in the list of players. So what you want to do is either keep updating the distance to a player with a loop or with an event.