Advertisement
Anukun_Lucifer

TowerScript

Nov 19th, 2023
2,827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | Gaming | 0 0
  1. local physicsservice = game:GetService("PhysicsService")
  2. local serverstorage = game:GetService("ServerStorage")
  3. local replicatedstorage = game:GetService("ReplicatedStorage")
  4.  
  5. local event = replicatedstorage:WaitForChild("Events")
  6. local spawntowerevent = event:WaitForChild("SpawnTower")
  7. local animatetowerevent = event:WaitForChild("AnimateTower")
  8.  
  9. local tower = {}
  10. --------------------------------------------
  11. local function FindNearestTarget(newTower)
  12.     local maxDistance = 20
  13.     local nearestTarget = nil
  14.  
  15.     for i, target in ipairs(workspace.Grassland.Mob:GetChildren()) do
  16.         local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
  17.         if distance < maxDistance then
  18.             nearestTarget = target
  19.             maxDistance = distance
  20.         end
  21.     end
  22.  
  23.     return nearestTarget
  24. end
  25.  
  26. function tower.Attack(newTower)
  27.     local target = FindNearestTarget(newTower)
  28.    
  29.     if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
  30.        
  31.         local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
  32.         newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
  33.        
  34.         animatetowerevent:FireAllClients(newTower, "Attack")
  35.         target.Humanoid:TakeDamage(25)
  36.  
  37.     end
  38.     task.wait(1)
  39.     return tower.Attack(newTower)
  40. end
  41. ---------------------------------------------
  42. function tower.Spawn(player, name, cframe)
  43.     local towerExists = replicatedstorage.Towers:FindFirstChild(name)
  44.  
  45.     if towerExists then
  46.         local newTower = towerExists:Clone()
  47.         newTower.HumanoidRootPart.CFrame = cframe
  48.         newTower.Parent = workspace.Grassland.Tower
  49.         newTower.HumanoidRootPart:SetNetworkOwner(nil)
  50.        
  51.         local bodyGyro = Instance.new("BodyGyro")
  52.         bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
  53.         bodyGyro.D = 0
  54.         bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
  55.         bodyGyro.Parent = newTower.HumanoidRootPart
  56.  
  57.         for i, object in ipairs(newTower:GetDescendants()) do
  58.             if object:IsA("BasePart") then
  59.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  60.             end
  61.         end
  62.        
  63.         coroutine.wrap(tower.Attack) (newTower)
  64.     else
  65.         warn("Requested mob dose not exist",name)
  66.     end
  67. end
  68.  
  69. spawntowerevent.OnServerEvent:Connect(tower.Spawn)
  70.  
  71. return tower
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement