Advertisement
Anukun_Lucifer

TowerScript (Module Script / EP.09)

Jan 20th, 2024
1,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | Gaming | 0 0
  1. local physicsservice = game:GetService("PhysicsService")
  2. local serverstorage = game:GetService("ServerStorage")
  3. local replicatedstorage = game:GetService("ReplicatedStorage")
  4. local functions = replicatedstorage:WaitForChild("Functions")
  5. local requestTowerFuntion = functions:WaitForChild("RequestTower")
  6.  
  7. local event = replicatedstorage:WaitForChild("Events")
  8. local spawntowerevent = event: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.     tower.Attack(newTower ,player)
  49. end
  50. ---------------------------------------------
  51. function tower.Spawn(player, name, cframe)
  52.     local allowedToSpawn = tower.CheckSpawn(player, name)
  53.  
  54.     if allowedToSpawn then
  55.         local newTower = replicatedstorage.Towers[name]:Clone()
  56.         newTower.HumanoidRootPart.CFrame = cframe
  57.         newTower.Parent = workspace.Grassland.Tower
  58.         newTower.HumanoidRootPart:SetNetworkOwner(nil)
  59.        
  60.         local bodyGyro = Instance.new("BodyGyro")
  61.         bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
  62.         bodyGyro.D = 0
  63.         bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
  64.         bodyGyro.Parent = newTower.HumanoidRootPart
  65.  
  66.         for i, object in ipairs(newTower:GetDescendants()) do
  67.             if object:IsA("BasePart") then
  68.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  69.             end
  70.         end
  71.        
  72.         player.Gold.Value -= newTower.Config.Price.Value
  73.         player.PlacedTowers.Value +=1
  74.        
  75.         coroutine.wrap(tower.Attack) (newTower ,player)
  76.     else
  77.         warn("Requested mob dose not exist",name)
  78.     end
  79. end
  80.  
  81. spawntowerevent.OnServerEvent:Connect(tower.Spawn)
  82.  
  83. function tower.CheckSpawn(player, name)
  84.     local towerExists = replicatedstorage.Towers:FindFirstChild(name)
  85.    
  86.     if towerExists then
  87.         if towerExists.Config.Price.Value <= player.Gold.Value then
  88.             print("okkkkkkkkkkkkkkkkkkkkkkkkkkkkkk")
  89.             if player.PlacedTowers.Value < maxTowers then
  90.                 return true
  91.             end
  92.         end
  93.     end
  94.     return false
  95. end
  96.  
  97. requestTowerFuntion.OnServerInvoke = tower.CheckSpawn
  98.  
  99. return tower
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement