I wanna make a basic A.I. for this bad zombie game (on purpose).
But it always throws the error in the title.
Mind you help?
This is a serverscript inside the model of the zombie (it is all unanchored)
local Players = game:GetService("Players")
local part = script.Parent:FindFirstChild('HumanoidRootPart') or script.Parent:FindFirstChildWhichIsA('Humanoid').RootPart
local maxDistance = 100
local getNearestPlayer = function()
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
if (player) then
local distance = player:DistanceFromCharacter(part.Position)
if not player.Character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
end
return nearestPlayer
end
local loop = coroutine.create(function()
while true do
task.wait(3)
local player = getNearestPlayer()
local walkToPart = player.Character:FindFirstChild('HumanoidRootPart') or player.Character:FindFirstChildWhichIsA('Humanoid').RootPart
script.Parent:FindFirstChildWhichIsA('Humanoid'):MoveTo(walkToPart.Position, walkToPart)
script.Parent:FindFirstChild('HumanoidRootPart'):SetNetworkOwner(nil)
end
end)
coroutine.resume(loop)
script.Parent:FindFirstChildWhichIsA('Humanoid').Died:Connect(function()
coroutine.close(loop)
end)
game:GetService('ReplicatedStorage'):FindFirstChild('MapStarted').Changed:Connect(function(v)
if (game:GetService('ReplicatedStorage'):FindFirstChild('MapStarted').Value == false) then
coroutine.close(loop)
end
end)
