Advertisement
GigaOrts

Zombie

Jun 8th, 2025 (edited)
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. -- PlayerConnect
  2. function whenLoaded(player)
  3.     warn("Игрок", player, "подключился к игре!")
  4.     script.Parent.Player.Value = player
  5.     print(script.Parent.Player.Value)
  6. end
  7. game.Players.PlayerAdded:Connect(whenLoaded)
  8.  
  9. --ZombieSpawn
  10. working = true
  11. local distance = 0
  12. while working do
  13.     for _, player in pairs(game.Players:GetPlayers()) do
  14.         distance = player:DistanceFromCharacter(script.Parent.Position)
  15.         if (distance > 0) and (distance < 50) then
  16.             working = false
  17.         end
  18.     end
  19.     wait(1)
  20. end
  21. wait(2)
  22. D_num = script.Parent.Parent.Quantity
  23. while D_num.Value < 10 do
  24.     if distance < 50 then
  25.         local zombie = game.ServerStorage.Zombie:Clone()
  26.         zombie.Parent = game.Workspace.NPC
  27.         zombie:MoveTo(script.Parent.Position)
  28.         D_num.Value += 1
  29.     end
  30.     local destination = game.Workspace.Data.Player.Value.Character.HumanoidRootPart.Position
  31.     distance = (destination - script.Parent.Position).magnitude
  32.     wait(5)
  33. end
  34.  
  35.  
  36. --SeekEnemy
  37. local npc = script.Parent --получаем модель НПС
  38. local humanoid = npc:WaitForChild("Humanoid") --получаем гуманоида из модели, с его помощью будем двигать НПС
  39. local rootPart = npc:WaitForChild("HumanoidRootPart") --получаем корневую деталь из модели, от неё будем брать позицию НПС
  40. local nearestPlayerPos = nil --создаем переменную для ближайшего игрока
  41. local shortestDistance = 100 -- Большое значение для сравнения
  42.  
  43. local function getNearestPlayerPos() --функция, которая ищет позицию ближайшего игрока
  44.     for _, player in ipairs(game.Players:GetPlayers()) do --перебираем всех игроков в игре
  45.         local character = player.Character --получаем модель игрока
  46.         if character and character:FindFirstChild("HumanoidRootPart") then --если модель найдена и имеет корневую деталь
  47.             local characterRootPos = character.HumanoidRootPart.Position --Получаем позицию игрока
  48.             local distance = (characterRootPos - rootPart.Position).Magnitude --считаем растояние между игроком и НПС
  49.             if distance < shortestDistance then
  50.                 shortestDistance = distance --сохраняем новое ближайшее растояние
  51.             end
  52.             if distance < 50 then --если растояние меньше 50 и меньше предыдущего, запоминаем его
  53.                 nearestPlayerPos = characterRootPos --сохраняем игрока как ближайшего
  54.             end
  55.         end
  56.     end
  57.     return nearestPlayerPos --возвращаем позицию ближайшего игрока
  58. end
  59.  
  60. while wait() do --запускаем бесконечный цикл
  61.     local nearestPlayerPos = getNearestPlayerPos() --получаем ближайшего игрока
  62.     if nearestPlayerPos then --если нашли ближайшего игрока
  63.         humanoid:MoveTo(nearestPlayerPos) --двигаем НПС к ближайшему игроку
  64.     end
  65. end
  66.  
  67. --ZombieAttack
  68. local rarm = script.Parent:FindFirstChild("RightHand")
  69. local larm = script.Parent:FindFirstChild("LeftHand")
  70. local torso = script.Parent:FindFirstChild("UpperTorso")
  71. local Attack_Speed = script.Parent.Parent.AttackSpeed
  72.  
  73. function dmg(hit)
  74.     if hit.Parent ~= nil then
  75.         local hum = hit.Parent:FindFirstChild("Humanoid")
  76.         if hum ~= nil then
  77.             hum.Health = hum.Health - 0.1
  78.         end
  79.         wait(Attack_Speed.Value)
  80.  
  81.     end
  82. end
  83.  
  84.  
  85. rarm.Touched:Connect(dmg)
  86. larm.Touched:Connect(dmg)
  87. torso.Touched:Connect(dmg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement