Advertisement
Anukun_Lucifer

Controller(EP.12)

May 12th, 2024
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.72 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 functions = replicatedstorage:WaitForChild("Functions")
  12. local requestTowerFuntion = functions:WaitForChild("RequestTower")
  13. local SellTowerFuntion = functions:WaitForChild("SellTower")
  14. local tower = replicatedstorage:WaitForChild("Towers")
  15. local spawntowerFuntion = functions: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 hoveredInstance = nil
  23. local selectedTower = nil
  24. local towertospawn = nil
  25. local canplace = false
  26. local rotation = 0
  27. local platedTower = 0
  28. local maxTowers = 10
  29.  
  30. local function SetupGui()
  31.     health.Setup(base,gui.Info.Health)
  32.  
  33.     workspace.Grassland.Mob.ChildAdded:Connect(function(mob)
  34.         health.Setup(mob)
  35.     end)
  36.  
  37.     info.Message.Changed:Connect(function(change)
  38.         gui.Info.Message.Text = change
  39.         if change == "" then
  40.             gui.Info.Message.Visible = false
  41.         else
  42.             gui.Info.Message.Visible = true
  43.         end
  44.     end)
  45.  
  46.     info.Wave.Changed:Connect(function(change)
  47.         gui.Info.Stat.Wave.Text = "Wave:"..change
  48.     end)
  49.  
  50.     gold.Changed:Connect(function(change)
  51.         gui.Info.Stat.Gold.Text = "$"..gold.Value
  52.     end)
  53.     gui.Info.Stat.Gold.Text = "$"..gold.Value
  54. end
  55.  
  56. SetupGui()
  57.  
  58. local function MouseRaycast(blacklist)
  59.     local mouseposition = userinputservice:GetMouseLocation()
  60.     local mouseray = camera:ViewportPointToRay(mouseposition.X,mouseposition.Y)
  61.     local raycastparams = RaycastParams.new()
  62.  
  63.     raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
  64.     raycastparams.FilterDescendantsInstances = blacklist
  65.  
  66.     local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction*1000, raycastparams)
  67.  
  68.     return raycastresult
  69. end
  70.  
  71. local function CreateRangeCircle(tower, placeholder)
  72.     local range = tower.Config.Range.Value
  73.     local height = (tower.PrimaryPart.Size.Y / 2) + tower.Humanoid.HipHeight
  74.     local offset = CFrame.new(0, -height,0)
  75.    
  76.     local p = Instance.new("Part")
  77.     p.Name = "Range"
  78.     p.Shape = Enum.PartType.Cylinder
  79.     p.Material = Enum.Material.SmoothPlastic
  80.     p.Color = Color3.new(1, 1, 1)
  81.     p.Transparency = 0.8
  82.     p.Size = Vector3.new(2 ,range *2, range*2)
  83.     p.TopSurface = Enum.SurfaceType.Smooth
  84.     p.BottomSurface = Enum.SurfaceType.Smooth
  85.     p.CFrame = tower.PrimaryPart.CFrame * offset * CFrame.Angles(0,0, math.rad(90))
  86.     p.CanCollide = false
  87.    
  88.     if placeholder then
  89.         p.Anchored = false
  90.         local weld = Instance.new("WeldConstraint")
  91.         weld.Part0 = p
  92.         weld.Part1 = tower.PrimaryPart
  93.         weld.Parent = p
  94.         p.Parent = tower
  95.     else
  96.         p.Anchored = true
  97.         p.Parent = workspace.Camera
  98.     end
  99.    
  100. end
  101.  
  102. local function RemovePlaceholderTower()
  103.     if towertospawn then
  104.         towertospawn:Destroy()
  105.         towertospawn = nil
  106.         rotation = 0
  107.     end
  108. end
  109.  
  110. local function AddPlaceholderTower(name)
  111.     local towerExists = tower:FindFirstChild(name)
  112.     if towerExists then
  113.         RemovePlaceholderTower()
  114.         towertospawn = towerExists:Clone()
  115.         towertospawn.Parent = workspace.Grassland
  116.        
  117.         CreateRangeCircle(towertospawn, true)
  118.  
  119.         for i, object in ipairs(towertospawn:GetDescendants()) do
  120.             if object:IsA("BasePart") then
  121.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  122.                 if object.Name ~= "Range"  then
  123.                     object.Material = Enum.Material.ForceField
  124.                     object.Transparency = 0.3  
  125.                 end
  126.             end
  127.         end
  128.     end
  129. end
  130.  
  131. local function ColorPlanceholderTower(color)
  132.     for i, object in ipairs(towertospawn:GetDescendants()) do
  133.         if object:IsA("BasePart") then
  134.             object.Color = color
  135.         end
  136.     end
  137. end
  138.  
  139. gui.Tower.Title.Text = "Towers"..platedTower.."/"..maxTowers
  140. for i , tower in pairs(tower:GetChildren()) do
  141.     if tower:IsA("Model") then
  142.         local button = gui.Tower.Template:Clone()
  143.         local config = tower:WaitForChild("Config")
  144.         button.Name = tower.Name
  145.         button.Image = config.Image.Texture
  146.         button.Visible = true
  147.  
  148.         button.LayoutOrder = config.Price.Value
  149.         button.Price.Text = config.Price.Value
  150.  
  151.         button.Parent = gui.Tower
  152.  
  153.         button.Activated:Connect(function()
  154.             local allowedToSpawn = requestTowerFuntion:InvokeServer(tower.Name)
  155.             if allowedToSpawn then
  156.                 AddPlaceholderTower(tower.Name)
  157.             end
  158.         end)
  159.     end
  160. end
  161.  
  162. local function toggleTowerInfo()
  163.     workspace.Camera:ClearAllChildren()
  164.     gui.Tower.Title.Text = "Towers"..platedTower.."/"..maxTowers
  165.     if selectedTower then
  166.         CreateRangeCircle(selectedTower)
  167.         gui.Selection.Visible = true
  168.         local config = selectedTower.Config
  169.         gui.Selection.Stat.Damage.Value.Text = config.Damage.Value
  170.         gui.Selection.Stat.Range.Value.Text = config.Range.Value
  171.         gui.Selection.Stat.Cooldown.Value.Text = config.Cooldown.Value
  172.         gui.Selection.Title.ImageLabel.Image = config.Image.Texture
  173.         gui.Selection.Title.TowerName.Text = selectedTower.Name
  174.        
  175.         local upgradeTower = config:FindFirstChild("Upgrade")
  176.         if upgradeTower then
  177.             gui.Selection.Action.Upgrade.Visible = true
  178.             gui.Selection.Action.Upgrade.Text = " Upgrade (" ..upgradeTower.Value.Config.Price.Value.. ") "
  179.         else
  180.             gui.Selection.Action.Upgrade.Visible = false
  181.         end
  182.     else
  183.         gui.Selection.Visible = false
  184.     end
  185. end
  186.  
  187. gui.Selection.Action.Upgrade.Activated:Connect(function()
  188.     if selectedTower then
  189.         local upgradeTower = selectedTower.Config.Upgrade.Value
  190.         local allowedToUpgrade = requestTowerFuntion:InvokeServer(upgradeTower.Name)
  191.        
  192.         if allowedToUpgrade then
  193.             selectedTower = spawntowerFuntion:InvokeServer(upgradeTower.Name, selectedTower.PrimaryPart.CFrame, selectedTower)
  194.             toggleTowerInfo()
  195.         end
  196.     end
  197. end)
  198.  
  199. gui.Selection.Action.Sell.Activated:Connect(function()
  200.     if selectedTower then
  201.         local soldTower = SellTowerFuntion:InvokeServer(selectedTower)
  202.         if soldTower then
  203.             selectedTower = nil
  204.             platedTower -= 1
  205.             toggleTowerInfo()
  206.         end
  207.     end
  208. end)
  209.  
  210. userinputservice.InputBegan:Connect(function(input, processed)
  211.     if processed then
  212.         return
  213.     end
  214.  
  215.     if towertospawn then
  216.         if input.UserInputType == Enum.UserInputType.MouseButton1 then
  217.             if canplace then
  218.                 local placedtower = spawntowerFuntion:InvokeServer(towertospawn.Name, towertospawn.PrimaryPart.CFrame)
  219.                 if platedTower then
  220.                     platedTower += 1
  221.                     RemovePlaceholderTower()
  222.                     toggleTowerInfo()
  223.                 end
  224.             end
  225.         elseif input.KeyCode == Enum.KeyCode.R then
  226.             rotation += 90
  227.         end
  228.     elseif hoveredInstance and input.UserInputType == Enum.UserInputType.MouseButton1 then
  229.         local model = hoveredInstance:FindFirstAncestorOfClass("Model")
  230.        
  231.         if model and model.Parent == workspace.Grassland.Tower then
  232.             selectedTower = model
  233.         else
  234.             selectedTower = nil
  235.         end
  236.         toggleTowerInfo()
  237.         print(selectedTower)
  238.     end
  239. end)
  240.  
  241. runservice.RenderStepped:Connect(function()
  242.     local result = MouseRaycast({towertospawn})
  243.     if result and result.Instance then
  244.         if towertospawn then
  245.             hoveredInstance = nil
  246.             if result.Instance.Parent.Name == "TowerArea" then
  247.                 canplace = true
  248.                 ColorPlanceholderTower(Color3.new(0,1,0))
  249.             else
  250.                 canplace = false
  251.                 ColorPlanceholderTower(Color3.new(1,0,0))
  252.             end
  253.             local x = result.Position.X
  254.             local y = result.Position.Y + towertospawn.Humanoid.HipHeight + 4
  255.             local z = result.Position.Z
  256.  
  257.             local cframe = CFrame.new(x,y,z) *CFrame.Angles(0,math.rad(rotation),0)
  258.             towertospawn:SetPrimaryPartCFrame(cframe)
  259.         else
  260.             hoveredInstance = result.Instance
  261.         end
  262.     else
  263.         hoveredInstance = nil
  264.     end
  265.     --print(hoveredInstance)
  266. end)
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement