Advertisement
Anukun_Lucifer

TowerScript(EP.11)

Mar 17th, 2024
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 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 functions = replicatedstorage:WaitForChild("Functions")
  6. local requestTowerFuntion = functions:WaitForChild("RequestTower")
  7. local event = replicatedstorage:WaitForChild("Events")
  8. local spawntowerFuntion = functions:WaitForChild("SpawnTower")
  9. local animatetowerevent = event:WaitForChild("AnimateTower")
  10. local maxTowers = 10
  11.  
  12. local tower = {}
  13. --------------------------------------------
  14. local function FindNearestTarget(newTower,range)
  15.     local nearestTarget = nil
  16.  
  17.     for i, target in ipairs(workspace.Grassland.Mob:GetChildren()) do
  18.         local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
  19.         if distance < range then
  20.             nearestTarget = target
  21.             range = distance
  22.         end
  23.     end
  24.  
  25.     return nearestTarget
  26. end
  27.  
  28. function tower.Attack(newTower ,player)
  29.     local config = newTower.Config
  30.     local target = FindNearestTarget(newTower,config.Range.Value)
  31.    
  32.     if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
  33.        
  34.         local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
  35.         newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
  36.        
  37.         animatetowerevent:FireAllClients(newTower, "Attack")
  38.         target.Humanoid:TakeDamage(config.Damage.Value)
  39.        
  40.         if target.Humanoid.Health <= 0 then
  41.             player.Gold.Value += target.Humanoid.MaxHealth
  42.         end
  43.        
  44.         task.wait(config.Cooldown.Value)
  45.        
  46.     end
  47.     task.wait(0.1)
  48.    
  49.     if newTower and newTower.Parent then
  50.         tower.Attack(newTower, player)
  51.     end
  52. end
  53. ---------------------------------------------
  54. function tower.Spawn(player, name, cframe, previous)
  55.     local allowedToSpawn = tower.CheckSpawn(player, name)
  56.  
  57.     if allowedToSpawn then
  58.        
  59.         local newTower
  60.         if previous then
  61.             previous:Destroy()
  62.             newTower = replicatedstorage.Towers.Upgrades[name]:Clone()
  63.         else
  64.             newTower = replicatedstorage.Towers[name]:Clone()
  65.         end
  66.  
  67.         newTower.HumanoidRootPart.CFrame = cframe
  68.         newTower.Parent = workspace.Grassland.Tower
  69.         newTower.HumanoidRootPart:SetNetworkOwner(nil)
  70.        
  71.         local bodyGyro = Instance.new("BodyGyro")
  72.         bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
  73.         bodyGyro.D = 0
  74.         bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
  75.         bodyGyro.Parent = newTower.HumanoidRootPart
  76.  
  77.         for i, object in ipairs(newTower:GetDescendants()) do
  78.             if object:IsA("BasePart") then
  79.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  80.             end
  81.         end
  82.        
  83.         player.Gold.Value -= newTower.Config.Price.Value
  84.         player.PlacedTowers.Value +=1
  85.        
  86.         coroutine.wrap(tower.Attack) (newTower ,player)
  87.        
  88.         return newTower
  89.     else
  90.         warn("Requested mob dose not exist",name)
  91.         return false
  92.     end
  93. end
  94.  
  95. spawntowerFuntion.OnServerInvoke = tower.Spawn
  96.  
  97. function tower.CheckSpawn(player, name)
  98.     local towerExists = replicatedstorage.Towers:FindFirstChild(name, true)
  99.    
  100.     if towerExists then
  101.         if towerExists.Config.Price.Value <= player.Gold.Value then
  102.             print("okkkkkkkkkkkkkkkkkkkkkkkkkkkkkk")
  103.             if player.PlacedTowers.Value < maxTowers then
  104.                 return true
  105.             end
  106.         end
  107.     end
  108.     return false
  109. end
  110.  
  111. requestTowerFuntion.OnServerInvoke = tower.CheckSpawn
  112.  
  113. return tower
  114.  
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement