Advertisement
Anukun_Lucifer

TowerScript(EP.12)

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