Hello Devforum! Recently, I have been working on a small zombie project of my own and I am working on making one zombie controller script that controls all the zombies in the game. I used this video: Roblox - Controlling Multiple AI with One Script (advanced) - Scripting Tutorial - YouTube to help me make it, but after I finished, nothing was working and no print statements were printed.
Here is a single zombie script inside a zombie:
local zombie = script.Parent
local zomhumroot = zombie.HumanoidRootPart
local zomanoid = zombie.Zombie
local anim = zomanoid.Walk
local anim2 = zomanoid.Attack
local anim3 = zomanoid.Idle
local loadanim = zomanoid:LoadAnimation(anim)
local loadanim2 = zomanoid:LoadAnimation(anim2)
local loadanim3 = zomanoid:LoadAnimation(anim3)
function findtarget()
local dist = 1000
local target = nil
for i, human in pairs(game.Workspace:GetChildren()) do
if human:FindFirstChild("Humanoid") and human:FindFirstChild("HumanoidRootPart") then
local humanoid = human:FindFirstChild("Humanoid")
local humroot = human:FindFirstChild("HumanoidRootPart")
if humanoid and humroot and human ~= zombie then
if humanoid.Health > 0 then
if (zomhumroot.Position - humroot.Position).magnitude < dist then
dist = (zomhumroot.Position - humroot.Position).magnitude
target = humroot
end
end
end
end
end
return target
end
while wait(1) do
local humroot = findtarget()
if humroot then
zomanoid:MoveTo(humroot.Position, humroot)
loadanim:Play()
if (zomhumroot.Position - humroot.Position).magnitude < 5 then
loadanim2:Play()
humroot.Parent.Humanoid:TakeDamage(15)
end
else
zomanoid:MoveTo(zomhumroot.Position + Vector3.new(math.random(-50,50),0, math.random(-50,50)), game.Workspace.Part)
loadanim:Stop()
loadanim3:Play()
wait(3)
loadanim3:Stop()
loadanim:Play()
end
end
And here is the current controller script that I am working on:
local collectionservice = game:GetService("CollectionService")
local zombies = {}
function spawner(func, param)
local co = coroutine.wrap(func)
co(param)
end
function findtarget()
local humans = collectionservice:GetTagged("Human")
for i, zombie in pairs(zombies) do
local dist = 1000
local target = nil
local zomanoid = zombie.Humanoid
local zomhumroot = zombie.HumanoidRootPart
for i, human in pairs(humans) do
if human:FindFirstChild("Humanoid") and human:FindFirstChild("HumanoidRootPart") then
local humanoid = human:FindFirstChild("Humanoid")
local humroot = human:FindFirstChild("HumanoidRootPart")
if humanoid and humroot and human ~= zombie then
if humanoid.Health > 0 then
if (zomhumroot.Position - humroot.Position).magnitude < dist then
dist = (zomhumroot.Position - humroot.Position).magnitude
target = humroot
end
end
end
end
end
return target
end
end
function movehandler(zombie)
local zomanoid = zombie.humanoid
local zomhumroot = zombie.humroot
local anim1 = zombie.walkanim
local anim2 = zombie.attackanim
local anim3 = zombie.idleanim
spawner(function()
while wait(1) do
local humroot = findtarget()
if humroot then
zomanoid:MoveTo(humroot.Position, humroot)
anim1:Play()
if (zomhumroot.Position - humroot.Position).magnitude < 5 then
anim2:Play()
humroot.Parent.Humanoid:TakeDamage(15)
end
else
zomanoid:MoveTo(zomhumroot.Position + Vector3.new(math.random(-50,50),0, math.random(-50,50)), game.Workspace.Part)
anim1:Stop()
anim3:Play()
wait(3)
anim3:Stop()
anim1:Play()
end
end
end)
end
function taghuman(obj)
local human = obj:FindFirstChildWhichIsA("Humanoid")
if human then
collectionservice:AddTag(human, "Human")
end
print("tagged "..obj)
end
function zombiebodyremoval(zombie)
local index = table.find(zombies, zombie)
table.remove(zombies, index)
wait(2)
zombie.char:Destroy()
end
function addzomb(zomanoid)
table.insert(zombies, {
char = zomanoid.Parent,
humanoid = zomanoid,
humroot = zomanoid.Parent.HumanoidRootPart,
target = nil,
walkanim = zomanoid:LoadAnimation(zomanoid.Walk),
attackanim = zomanoid:LoadAnimation(zomanoid.Attack),
idleanim = zomanoid:LoadAnimation(zomanoid.Idle)
})
for i, zombie in pairs(zombies) do
if zombie.humanoid == zomanoid then
zombie.humanoid.Died:Connect(function() zombiebodyremoval(zombie) end)
for i, part in pairs(zombie.char:GetDescendants()) do
if part:IsA("BasePart") and part:SetNetworkOwnership() then
part:SetNetworkOwner(nil)
end
end
spawner(movehandler, zombie)
break
end
end
end
game.Workspace.ChildAdded:Connect(taghuman)
collectionservice:GetInstanceAddedSignal("Zombie"):Connect(function(zomanoid)
addzomb(zomanoid)
print("Added zomb tag to zomanoid")
end)
function initialize()
for i, zomtag in pairs(collectionservice:GetTagged("Zombie")) do
local found = false
for i, zombie in pairs(zombies) do
if zombie.humanoid == zomtag then
found = true
print("Found true")
end
end
if not found then
addzomb(zomtag)
print("Added to zombie")
end
end
end
initialize()
I am not sure what I did wrong because no print statements are printing. Please help!