Advertisement
LuaWeaver

Zombie manager example

Jul 16th, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. --zombie manager
  2. --put all zombies into workspace.Zombies
  3. --to add new zombies, just insert humanoids into workspace.Zombies
  4.  
  5. local zombies={}
  6. local zombieRange=50
  7.  
  8. function moveZombie(zombie,location)
  9.     zombie.Humanoid:MoveTo(location) -- MoveTo no longer requires a part as an argument
  10. end
  11.  
  12. function unregisterZombie(zombie)
  13.     for i,v in pairs(zombies) do
  14.         if v==zombie then
  15.             table.remove(zombies,i)
  16.             break
  17.         end
  18.     end
  19. end
  20.  
  21. function registerZombie(zombie) -- add a new zombie
  22.     assert(zombie:FindFirstChild("Humanoid")~=nil,"Zombie does not have a humanoid.") -- this line makes sure it has a humanoid
  23.     assert(zombie:FindFirstChild("Torso")~=nil,"Zombies does not have a torso.") -- similar to above
  24.     zombie.Humanoid.Died:connect(function()
  25.         unregisterZombie(zombie) -- remove this zombie from the list because it died
  26.     end)
  27.     zombies[#zombies+1]=zombie
  28. end
  29.  
  30. for i,v in pairs(workspace.Zombies:GetChildren()) do
  31.     registerZombie(v)
  32. end
  33. workspace.Zombies.ChildAdded:connect(registerZombie)
  34. workspace.Zombies.ChildRemoved:connect(unregisterZombie)
  35.  
  36. --main loop
  37. while wait(0.1) do --change 0.1 to something higher if you're having lag caused by this script
  38.     for _,zombie in pairs(zombies) do
  39.         if not zombie:FindFirstChild("Torso") or not zombie:FindFirstChild("Humanoid") then --if the torso or humanoid was somehow removed :(
  40.             unregisterZombie(zombie)
  41.         else
  42.             local zombiePos=zombie.Torso.Position
  43.             local nearestPlayerPos
  44.             local nearestDistance=zombieRange
  45.             for _,plr in pairs(game.Players:GetChildren()) do
  46.                 if plr.Character and plr.Character:FindFirstChild("Torso") then
  47.                     local playerPos=plr.Character.Torso.Position
  48.                     if (playerPos-zombiePos).magnitude<=nearestDistance then
  49.                         nearestPlayerPos=playerPos
  50.                         nearestDistance=(playerPos-zombiePos).magnitude
  51.                     end
  52.                 end
  53.             end
  54.             if nearestPlayerPos then -- if a player was found near enough to the zombie
  55.                 moveZombie(zombie,nearestPlayerPos)
  56.             end
  57.         end
  58.     end
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement