Advertisement
Anukun_Lucifer

Controller(EP.10)

Feb 11th, 2024
1,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | Gaming | 0 0
  1. local Players = game:GetService("Players")
  2. local physicsservice = game:GetService("PhysicsService")
  3. local replicatedstorage = game:GetService("ReplicatedStorage")
  4. local runservice = game:GetService("RunService")
  5. local userinputservice = game:GetService("UserInputService")
  6.  
  7. local modules = replicatedstorage:WaitForChild("Module")
  8. local health = require(modules:WaitForChild("HealthScript"))
  9.  
  10. local gold = Players.LocalPlayer:WaitForChild("Gold")
  11. local events = replicatedstorage:WaitForChild("Events")
  12. local functions = replicatedstorage:WaitForChild("Functions")
  13. local requestTowerFuntion = functions:WaitForChild("RequestTower")
  14. local tower = replicatedstorage:WaitForChild("Towers")
  15. local spawntowerevent = events:WaitForChild("SpawnTower")
  16. local camera = workspace.CurrentCamera
  17. local gui = script.Parent
  18. local map = workspace:WaitForChild("Grassland")
  19. local base = map:WaitForChild("Base")
  20. local info = workspace:WaitForChild("Info")
  21.  
  22. local towertospawn = nil
  23. local canplace = false
  24. local rotation = 0
  25. local platedTower = 0
  26. local maxTowers = 10
  27.  
  28. local function SetupGui()
  29.     health.Setup(base,gui.Info.Health)
  30.  
  31.     workspace.Grassland.Mob.ChildAdded:Connect(function(mob)
  32.         health.Setup(mob)
  33.     end)
  34.    
  35.     info.Message.Changed:Connect(function(change)
  36.         gui.Info.Message.Text = change
  37.         if change == "" then
  38.             gui.Info.Message.Visible = false
  39.         else
  40.             gui.Info.Message.Visible = true
  41.         end
  42.     end)
  43.    
  44.     info.Wave.Changed:Connect(function(change)
  45.         gui.Info.Stat.Wave.Text = "wave:"..change
  46.     end)
  47.    
  48.     gold.Changed:Connect(function(change)
  49.         gui.Info.Stat.Gold.Text = "$"..gold.Value
  50.     end)
  51.     gui.Info.Stat.Gold.Text = "$"..gold.Value
  52. end
  53.  
  54. SetupGui()
  55.  
  56. local function MouseRaycast(blacklist)
  57.     local mouseposition = userinputservice:GetMouseLocation()
  58.     local mouseray = camera:ViewportPointToRay(mouseposition.X,mouseposition.Y)
  59.     local raycastparams = RaycastParams.new()
  60.  
  61.     raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
  62.     raycastparams.FilterDescendantsInstances = blacklist
  63.  
  64.     local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction*1000, raycastparams)
  65.  
  66.     return raycastresult
  67. end
  68.  
  69. local function RemovePlaceholderTower()
  70.     if towertospawn then
  71.         towertospawn:Destroy()
  72.         towertospawn = nil
  73.         rotation = 0
  74.     end
  75. end
  76.  
  77. local function AddPlaceholderTower(name)
  78.     local towerExists = tower:FindFirstChild(name)
  79.     if towerExists then
  80.         RemovePlaceholderTower()
  81.         towertospawn = towerExists:Clone()
  82.         towertospawn.Parent = workspace
  83.  
  84.         for i, object in ipairs(towertospawn:GetDescendants()) do
  85.             if object:IsA("BasePart") then
  86.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  87.                 object.Material = Enum.Material.ForceField
  88.             end
  89.         end
  90.     end
  91. end
  92.  
  93. local function ColorPlanceholderTower(color)
  94.     for i, object in ipairs(towertospawn:GetDescendants()) do
  95.         if object:IsA("BasePart") then
  96.             object.Color = color
  97.         end
  98.     end
  99. end
  100.  
  101. gui.Tower.Title.Text = "Towers"..platedTower.."/"..maxTowers
  102. for i , tower in pairs(tower:GetChildren()) do
  103.     local button = gui.Tower.Template:Clone()
  104.     local config = tower:WaitForChild("Config")
  105.     button.Name = tower.Name
  106.     button.Image = config.Image.Texture
  107.     button.Visible = true
  108.    
  109.     button.LayoutOrder = config.Price.Value
  110.     button.Price.Text = config.Price.Value
  111.    
  112.     button.Parent = gui.Tower
  113.    
  114.     button.Activated:Connect(function()
  115.         local allowedToSpawn = requestTowerFuntion:InvokeServer(tower.Name)
  116.         if allowedToSpawn then
  117.             AddPlaceholderTower(tower.Name)
  118.         end
  119.     end)
  120. end
  121.  
  122. userinputservice.InputBegan:Connect(function(input, processed)
  123.     if processed then
  124.         return
  125.     end
  126.  
  127.     if towertospawn then
  128.         if input.UserInputType == Enum.UserInputType.MouseButton1 then
  129.             if canplace then
  130.                 spawntowerevent:FireServer(towertospawn.Name, towertospawn.PrimaryPart.CFrame)
  131.                 platedTower += 1
  132.                 gui.Tower.Title.Text = "Towers"..platedTower.."/"..maxTowers
  133.                 RemovePlaceholderTower()
  134.             end
  135.         elseif input.KeyCode == Enum.KeyCode.R then
  136.             rotation += 90
  137.         end
  138.     end
  139.  
  140. end)
  141.  
  142. runservice.RenderStepped:Connect(function()
  143.     if towertospawn then
  144.         local result = MouseRaycast({towertospawn})
  145.         if result and result.Instance then
  146.             if result.Instance.Parent.Name == "TowerArea" then
  147.                 canplace = true
  148.                 ColorPlanceholderTower(Color3.new(0,1,0))
  149.             else
  150.                 canplace = false
  151.                 ColorPlanceholderTower(Color3.new(1,0,0))
  152.             end
  153.             local x = result.Position.X
  154.             local y = result.Position.Y + towertospawn.Humanoid.HipHeight + 4
  155.             local z = result.Position.Z
  156.  
  157.             local cframe = CFrame.new(x,y,z) *CFrame.Angles(0,math.rad(rotation),0)
  158.             towertospawn:SetPrimaryPartCFrame(cframe)
  159.         end
  160.     end
  161. end)
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement