dunno why, but this wont work. looking for an answer, thanks if u can help. btw, anything doesnt come in the output. i also found out that it doesn't tag the zombie's humanoid.
local CollectionService = game:GetService("CollectionService")
local zombies = {}
function updateTarget()
local humans = CollectionService:GetTagged("Humans")
for _, zombie in pairs(zombies) do
local target = nil
local dist = 200
for _, human in pairs(humans) do
local root = human.rootPart
if root and human.Humanoid.Health > 0 and (root.Position - zombie.root.Position).magnitude and human.Parent.Name ~= zombie.char.Name then
local target = root
local dist = (root.Position - zombie.root.Position).magnitude
end
end
end
end
spawn(function()
while wait(1) do
updateTarget()
end
end)
function findPath(zombie)
local x = math.random(-50,-50)
local z = math.random(-50,-50)
local destination = Vector3.new(x,0,z)
local path = game:GetService("PathfindingService"):CreatePath()
path:ComputeAsync(zombie.root.Position, destination)
local waypoints = path:GetWaypoints()
local currentTarget = zombie.target
for _, waypoint in pairs(waypoints) do
if path.Status == Enum.PathStatus.Success then
if waypoint.Action == Enum.PathWaypointAction.Jump then
zombie.human.Jump = true
end
zombie.human:MoveTo(Vector3.new(waypoint.Position))
local cooldown = zombie.human.MoveToFinished:Wait(1)
if not cooldown then
print("Got stuck!")
zombie.human.Jump = true
findPath(zombie)
break
end
if not zombie.target then
break
elseif (currentTarget.Position - waypoints[#waypoints].Position).magnitude > 10 or currentTarget ~= zombie.Target then
findPath(zombie)
break
end
end
print("Path unsuccessful.")
zombie.human.Jump = true
findPath()
end
end
function moveHandler(zombie)
if zombie.human.Health > 0 and zombie.target then
findPath(zombie)
end
end
function tagHuman(instance)
local human = instance:FindFirstChildWhichIsA("Humanoid")
if human then
CollectionService:AddTag(human,"Humans")
end
end
workspace.ChildAdded:Connect(tagHuman)
function removeZombie(zombie)
local index = table.find(zombies, zombie)
table.remove(zombies,index)
wait(5)
zombie.char:Destroy()
zombie.clone.Parent = workspace
end
function addZombie(zombieHumanoid)
table.insert(zombies, {
char = zombieHumanoid.Parent,
root = zombieHumanoid.RootPart,
human = zombieHumanoid,
target = nil,
clone = zombieHumanoid.Parent:Clone()
})
for _, zombie in pairs(zombies) do
if zombie.human == zombieHumanoid then
zombieHumanoid.Died:Connect(removeZombie(zombie))
for _, v in pairs(zombie.char:GetDescendants()) do
if v:IsA("BasePart") and v:CanSetNetworkOwnership() then
v:SetNetworkOwnership(nil)
end
end
spawn(moveHandler, zombie)
end
end
end
CollectionService:GetInstanceAddedSignal("Zombie"):Connect(function(zombieHumanoid)
addZombie(zombieHumanoid)
end)
function initialize()
for _, v in pairs(CollectionService:GetTagged("Zombie")) do
local found = false
for _, x in pairs(zombies) do
if x.human == v then
found = true
end
end
if not found then
addZombie(v)
end
end
end
function attack(zombie)
local human = zombie.target.Parent:WaitForChild("Humanoid")
human:TakeDamage(math.random(15,25))
end
spawn(function()
while wait(0.5) do
for _, zombie in pairs(zombies) do
if zombie.target then
if (zombie.target.Position - zombie.root.Position).magnitude < 5 then
attack(zombie)
end
end
end
end
end)
