Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --zombie manager
- --put all zombies into workspace.Zombies
- --to add new zombies, just insert humanoids into workspace.Zombies
- local zombies={}
- local zombieRange=50
- function moveZombie(zombie,location)
- zombie.Humanoid:MoveTo(location) -- MoveTo no longer requires a part as an argument
- end
- function unregisterZombie(zombie)
- for i,v in pairs(zombies) do
- if v==zombie then
- table.remove(zombies,i)
- break
- end
- end
- end
- function registerZombie(zombie) -- add a new zombie
- assert(zombie:FindFirstChild("Humanoid")~=nil,"Zombie does not have a humanoid.") -- this line makes sure it has a humanoid
- assert(zombie:FindFirstChild("Torso")~=nil,"Zombies does not have a torso.") -- similar to above
- zombie.Humanoid.Died:connect(function()
- unregisterZombie(zombie) -- remove this zombie from the list because it died
- end)
- zombies[#zombies+1]=zombie
- end
- for i,v in pairs(workspace.Zombies:GetChildren()) do
- registerZombie(v)
- end
- workspace.Zombies.ChildAdded:connect(registerZombie)
- workspace.Zombies.ChildRemoved:connect(unregisterZombie)
- --main loop
- while wait(0.1) do --change 0.1 to something higher if you're having lag caused by this script
- for _,zombie in pairs(zombies) do
- if not zombie:FindFirstChild("Torso") or not zombie:FindFirstChild("Humanoid") then --if the torso or humanoid was somehow removed :(
- unregisterZombie(zombie)
- else
- local zombiePos=zombie.Torso.Position
- local nearestPlayerPos
- local nearestDistance=zombieRange
- for _,plr in pairs(game.Players:GetChildren()) do
- if plr.Character and plr.Character:FindFirstChild("Torso") then
- local playerPos=plr.Character.Torso.Position
- if (playerPos-zombiePos).magnitude<=nearestDistance then
- nearestPlayerPos=playerPos
- nearestDistance=(playerPos-zombiePos).magnitude
- end
- end
- end
- if nearestPlayerPos then -- if a player was found near enough to the zombie
- moveZombie(zombie,nearestPlayerPos)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement