Advertisement
Anukun_Lucifer

StructureHandler (Local Script)

Feb 19th, 2023
1,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | Gaming | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local PlaceStructure = ReplicatedStorage:WaitForChild("PlaceStructure")
  3. local Structures = ReplicatedStorage:WaitForChild("Structures")
  4.  
  5. local UIS = game:GetService("UserInputService")
  6. local RunService = game:GetService("RunService")
  7.  
  8. local player = game.Players.LocalPlayer
  9. local StructureFrame = script.Parent.StructureFrame
  10. local Char = player.Character or player.Character:Wait()
  11. local HumanoidRootPart = Char:WaitForChild("HumanoidRootPart")
  12.  
  13. local mouse = player:GetMouse()
  14.  
  15. local yBuildingOffset = 5
  16. local maxPlacingDitance = 50
  17. local rKeyIsPressed = false
  18. local placingStructure = false
  19.  
  20. for _, structureButton in pairs(StructureFrame:GetChildren()) do
  21.     if structureButton:IsA("TextButton") then
  22.         structureButton.MouseButton1Click:Connect(function()
  23.            
  24.             StructureFrame.Visible = false
  25.            
  26.             local yOrientation = 0
  27.             local goodToPlace = false
  28.             local placedStructure
  29.            
  30.             if placingStructure == false then
  31.                 placingStructure = true
  32.                
  33.                 local clientStructure = Structures:FindFirstChild(structureButton.Name):Clone()
  34.                 clientStructure.BrickColor = BrickColor.new("Forest green")
  35.                 clientStructure.Material = "Neon"
  36.                 clientStructure.CanCollide = false
  37.                 clientStructure.Parent = game.Workspace
  38.                
  39.                 local startingCFrame = CFrame.new(0,-2,-15)
  40.                 clientStructure.CFrame = HumanoidRootPart.CFrame:ToWorldSpace(startingCFrame)
  41.                
  42.                 RunService.RenderStepped:Connect(function()
  43.                     local mouseRay = mouse.UnitRay
  44.                     local castRay = Ray.new(mouseRay.Origin,mouseRay.Direction * 1000)
  45.                     local ignoreList = {clientStructure,Char}
  46.                     local hit, position = workspace:FindPartOnRayWithIgnoreList(castRay,ignoreList)
  47.                    
  48.                     if hit and (hit:IsA("Terrain") or hit.Name:lower() == "terrain") and (HumanoidRootPart.Position - clientStructure.Position).Magnitude < maxPlacingDitance then
  49.                         goodToPlace = true
  50.                         clientStructure.BrickColor = BrickColor.new("Forest green")
  51.                     else
  52.                         goodToPlace = false
  53.                         clientStructure.BrickColor = BrickColor.new("Crimson")
  54.                        
  55.                     end
  56.                    
  57.                     local newAnglesCFrame = CFrame.Angles(0,math.rad(yOrientation),0)
  58.                     local newCFrame = CFrame.new(position.X,position.Y + yBuildingOffset,position.Z)
  59.                     clientStructure.CFrame = newCFrame * newAnglesCFrame
  60.                 end)
  61.                
  62.                 UIS.InputBegan:Connect(function(input)
  63.                     if input.KeyCode == Enum.KeyCode.R then
  64.                         rKeyIsPressed = true
  65.                        
  66.                         local rotationSpeed = 5
  67.                         while rKeyIsPressed do
  68.                             wait()
  69.                             if placingStructure == true then
  70.                                 yOrientation = yOrientation + rotationSpeed
  71.                             end
  72.                         end
  73.                     end
  74.                 end)
  75.                
  76.                 UIS.InputEnded:Connect(function(input)
  77.                     if input.KeyCode == Enum.KeyCode.R then
  78.                         rKeyIsPressed = false
  79.                     end
  80.                 end)
  81.                
  82.                 UIS.InputBegan:Connect(function(input)
  83.                     if input.UserInputType == Enum.UserInputType.MouseButton1 then
  84.                         if placingStructure == true then
  85.                             if goodToPlace == true then
  86.                                 local StructureCFrame = clientStructure.CFrame
  87.                                 placedStructure = PlaceStructure:InvokeServer(clientStructure.Name,StructureCFrame)
  88.                                
  89.                                 if placedStructure == true then
  90.                                     placingStructure = false
  91.                                     clientStructure:Destroy()
  92.                                     StructureFrame.Visible = true
  93.                                 end
  94.                                
  95.                             end
  96.                         end
  97.                     end
  98.                 end)
  99.             end
  100.            
  101.         end)
  102.     end
  103. end
Tags: Roblox
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement