local fDistance = 40
local Zombie = script.parent
while true do
local closestPlayer = nil;
local closestdis = fDistance;
for i, v in pairs(game.Players:GetChildren()) do
local human = v.Character:FindFirstChild("HumanoidRootPart")
if human then
if (human.Position - Zombie:FindFirstChild("ZTorso").Position) <= fDistance then
closestPlayer = v
closestdis = (human.Position-Zombie:FindFirstChild("ZTorso").Position).magnitude
end
end
end
print(closestPlayer.Name)
closestdis = (closestPlayer:FindFirstChild("HumanoidRootPart").Position-Zombie:FindFirstChild("ZTorso").Position).magnitude
Zombie:FindFirstChild("Humanoid"):MoveTo(closestPlayer.Character:FindFirstChild("HumanoidRootPart").Position, closestPlayer.Character:FindFirstChild("HumanoidRootPart"))
wait()
end
Why is this returning nil I'm telling it to always check meaning it shouldn't be nil I have a loop?
Workspace.Zombie.FollowPlayer:15: attempt to index local 'closestPlayer' (a nil value)
