Advertisement
Anukun_Lucifer

Controller

Nov 5th, 2023
4,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 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.  
  16. local function MouseRaycast(blacklist)
  17.     local mouseposition = userinputservice:GetMouseLocation()
  18.     local mouseray = camera:ViewportPointToRay(mouseposition.X,mouseposition.Y)
  19.     local raycastparams = RaycastParams.new()
  20.  
  21.     raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
  22.     raycastparams.FilterDescendantsInstances = blacklist
  23.  
  24.     local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction*1000, raycastparams)
  25.  
  26.     return raycastresult
  27. end
  28.  
  29. local function RemoveOlaceholderTower()
  30.     if towertospawn then
  31.         towertospawn:Destroy()
  32.         towertospawn = nil
  33.     end
  34. end
  35.  
  36. local function AddPlaceholderTower(name)
  37.     local towerExists = tower:FindFirstChild(name)
  38.     if towerExists then
  39.         RemoveOlaceholderTower()
  40.         towertospawn = towerExists:Clone()
  41.         towertospawn.Parent = workspace.Grassland.Tower
  42.  
  43.         for i, object in ipairs(towertospawn:GetDescendants()) do
  44.             if object:IsA("BasePart") then
  45.                 physicsservice:SetPartCollisionGroup(object, "Tower")
  46.                 object.Material = Enum.Material.ForceField
  47.             end
  48.         end
  49.     end
  50. end
  51.  
  52. local function ColorPlanceholderTower(color)
  53.     for i, object in ipairs(towertospawn:GetDescendants()) do
  54.         if object:IsA("BasePart") then
  55.             object.Color = color
  56.         end
  57.     end
  58. end
  59.  
  60. gui.TVManSpawn.Activated:Connect(function()
  61.     AddPlaceholderTower("TVman")
  62. end)
  63.  
  64. userinputservice.InputBegan:Connect(function(input, processed)
  65.     if processed then
  66.         return
  67.     end
  68.  
  69.     if towertospawn then
  70.         if input.UserInputType == Enum.UserInputType.MouseButton1 then
  71.             if canplace then
  72.                 spawntowerevent:FireServer(towertospawn.Name, towertospawn.PrimaryPart.CFrame)
  73.                 RemoveOlaceholderTower()
  74.             end
  75.         elseif input.KeyCode == Enum.KeyCode.R then
  76.             rotation += 90
  77.         end
  78.     end
  79.  
  80. end)
  81.  
  82. runservice.RenderStepped:Connect(function()
  83.     if towertospawn then
  84.         local result = MouseRaycast({towertospawn})
  85.         if result and result.Instance then
  86.             if result.Instance.Parent.Name == "TowerArea" then
  87.                 canplace = true
  88.                 ColorPlanceholderTower(Color3.new(0,1,0))
  89.             else
  90.                 canplace = false
  91.                 ColorPlanceholderTower(Color3.new(1,0,0))
  92.             end
  93.             local x = result.Position.X
  94.             local y = result.Position.Y + towertospawn.Humanoid.HipHeight + 4
  95.             local z = result.Position.Z
  96.  
  97.             local cframe = CFrame.new(x,y,z) *CFrame.Angles(0,math.rad(rotation),0)
  98.             towertospawn:SetPrimaryPartCFrame(cframe)
  99.         end
  100.     end
  101. end)
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement