Advertisement
Anukun_Lucifer

Controller (Local Script / EP.08)

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