Advertisement
Anukun_Lucifer

Controller(EP.11)

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