Advertisement
Anukun_Lucifer

Controller (Local Script / EP.09)

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