Advertisement
HowToRoblox

ConveyorGuiHandler

Jan 26th, 2023
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.90 KB | None | 0 0
  1. local rs = game:GetService("RunService")
  2. local uis = game:GetService("UserInputService")
  3.  
  4. local cam = workspace.CurrentCamera
  5. local mouse = game.Players.LocalPlayer:GetMouse()
  6.  
  7. local selectedConveyor = nil
  8. local currentRotation = 0
  9.  
  10. local conveyors = game:GetService("ReplicatedStorage"):WaitForChild("ConveyorSystemReplicatedStorage"):WaitForChild("Conveyors")
  11. local placeRemote = game:GetService("ReplicatedStorage")["ConveyorSystemReplicatedStorage"]:WaitForChild("PlaceConveyorEvent")
  12. local removeRemote = game:GetService("ReplicatedStorage")["ConveyorSystemReplicatedStorage"]:WaitForChild("RemoveConveyorEvent")
  13.  
  14. local openBtn = script.Parent:WaitForChild("ConveyorButton")
  15. local frame = script.Parent:WaitForChild("ConveyorFrame")
  16. frame.Visible = false
  17. local scrollingFrame = frame:WaitForChild("ConveyorList")
  18.  
  19.  
  20. function createConveyorFrame()
  21.    
  22.     for _, child in pairs(scrollingFrame:GetChildren()) do
  23.         if child.ClassName == script:WaitForChild("ConveyorItem").ClassName then
  24.             child:Destroy()
  25.         end
  26.     end
  27.    
  28.     local conveyorButtons = {}
  29.    
  30.     for _, conveyor in pairs(conveyors:GetChildren()) do
  31.        
  32.         local newButton = script:WaitForChild("ConveyorItem"):Clone()
  33.        
  34.         local conveyorName = conveyor.Name
  35.         newButton.ConveyorName.Text = conveyorName
  36.        
  37.         local conveyorModel = conveyor:Clone()
  38.         conveyorModel:PivotTo(CFrame.new())
  39.         conveyorModel.Parent = newButton.ConveyorImage
  40.        
  41.         local currentCamera = Instance.new("Camera")
  42.         currentCamera.CFrame = CFrame.new(conveyorModel:GetPivot().Position + Vector3.new(4, 3, 4), conveyorModel:GetPivot().Position + Vector3.new(0, -2, 0))
  43.         newButton.ConveyorImage.CurrentCamera = currentCamera
  44.         currentCamera.Parent = newButton.ConveyorImage
  45.        
  46.         newButton.MouseButton1Click:Connect(function()
  47.             if selectedConveyor == conveyor then
  48.                 selectedConveyor = nil
  49.             else
  50.                 selectedConveyor = conveyor
  51.             end
  52.         end)
  53.        
  54.         table.insert(conveyorButtons, {conveyor.Configuration.Speed.Value, newButton})
  55.     end
  56.    
  57.     table.sort(conveyorButtons, function(a, b)
  58.         return a[1] > b[1]
  59.     end)
  60.    
  61.     for _, conveyorButton in pairs(conveyorButtons) do
  62.         conveyorButton[2].Parent = scrollingFrame
  63.     end
  64. end
  65.  
  66. createConveyorFrame()
  67. conveyors.ChildAdded:Connect(createConveyorFrame)
  68. conveyors.ChildRemoved:Connect(createConveyorFrame)
  69.  
  70.  
  71. openBtn.MouseButton1Click:Connect(function()
  72.     frame.Visible = not frame.Visible
  73.    
  74.     if frame.Visible == false then
  75.         selectedConveyor = nil
  76.     end
  77. end)
  78.  
  79.  
  80. uis.InputBegan:Connect(function(inp, p)
  81.  
  82.     if p then return end
  83.  
  84.     if inp.KeyCode == Enum.KeyCode.R then
  85.         currentRotation += 90
  86.        
  87.     elseif inp.KeyCode == Enum.KeyCode.X then
  88.        
  89.         local rayparams = RaycastParams.new()
  90.         rayparams.FilterType = Enum.RaycastFilterType.Whitelist
  91.         rayparams.FilterDescendantsInstances = {workspace:WaitForChild("CREATED CONVEYORS")}
  92.         local mouseRay = workspace:Raycast(cam.CFrame.Position, CFrame.new(cam.CFrame.Position, mouse.Hit.Position).LookVector * 100, rayparams)
  93.  
  94.         if mouseRay then
  95.  
  96.             local conveyor = mouseRay.Instance.Parent
  97.             while conveyor.Parent ~= workspace["CREATED CONVEYORS"] do
  98.                 conveyor = conveyor.Parent
  99.             end
  100.  
  101.             if conveyor.CREATOR.Value == game.Players.LocalPlayer.UserId then
  102.                 removeRemote:FireServer(conveyor)
  103.             end
  104.         end
  105.     end
  106. end)
  107.  
  108.  
  109. mouse.Button1Up:Connect(function()
  110.    
  111.     if selectedConveyor then   
  112.         local conveyorModel = cam:FindFirstChild("SELECTED CONVEYOR")
  113.         if conveyorModel then
  114.            
  115.             local overlapparams = OverlapParams.new()
  116.             overlapparams.FilterType = Enum.RaycastFilterType.Whitelist
  117.             overlapparams.FilterDescendantsInstances = {workspace:WaitForChild("CREATED CONVEYORS")}
  118.             local partsInConveyor = workspace:GetPartBoundsInBox(conveyorModel.Conveyor.CFrame, conveyorModel.Conveyor.Size, overlapparams)
  119.  
  120.             local conveyorBlocked = false
  121.  
  122.             for _, part in pairs(partsInConveyor) do
  123.                 if part.Name == "Conveyor" then
  124.                     conveyorBlocked = true
  125.                     break
  126.                 end
  127.             end
  128.            
  129.             local raycastparams = RaycastParams.new()
  130.             raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
  131.             local downRay = workspace:Raycast(conveyorModel:GetPivot().Position, Vector3.new(0, -100, 0), raycastparams)
  132.  
  133.             if not downRay or downRay.Instance:FindFirstAncestorWhichIsA("Folder") == workspace:WaitForChild("CREATED CONVEYORS") then
  134.                 conveyorBlocked = true
  135.             end
  136.            
  137.             if not conveyorBlocked then
  138.                
  139.                 local pos = conveyorModel:GetPivot().Position
  140.                 local rot = currentRotation
  141.                
  142.                 placeRemote:FireServer(selectedConveyor, pos, rot)
  143.             end
  144.         end
  145.     end
  146. end)
  147.  
  148.  
  149. rs.Heartbeat:Connect(function()
  150.    
  151.     if selectedConveyor then
  152.        
  153.         local conveyorModel = nil
  154.        
  155.         if cam:FindFirstChild("SELECTED CONVEYOR") and cam["SELECTED CONVEYOR"].ConveyorName.Value == selectedConveyor.Name then
  156.             conveyorModel = cam["SELECTED CONVEYOR"]
  157.            
  158.         else
  159.            
  160.             if cam:FindFirstChild("SELECTED CONVEYOR") then
  161.                 cam["SELECTED CONVEYOR"]:Destroy()
  162.             end
  163.            
  164.             conveyorModel = selectedConveyor:Clone()
  165.            
  166.             for _, desc in pairs(conveyorModel:GetDescendants()) do
  167.                 if desc:IsA("BasePart") then
  168.                    
  169.                     desc.CanCollide = false
  170.                     desc.Transparency = 0.7
  171.                    
  172.                     local r = Instance.new("NumberValue")
  173.                     r.Name = "R"
  174.                     r.Value = desc.Color.R
  175.                     r.Parent = desc
  176.                     local g = Instance.new("NumberValue")
  177.                     g.Name = "G"
  178.                     g.Value = desc.Color.G
  179.                     g.Parent = desc
  180.                     local b = Instance.new("NumberValue")
  181.                     b.Name = "B"
  182.                     b.Value = desc.Color.B
  183.                     b.Parent = desc
  184.                    
  185.                     desc.Color = Color3.new(r.Value, math.clamp(g.Value + 0.3, 0, 1), b.Value)
  186.                 end
  187.             end
  188.            
  189.             local nameValue = Instance.new("StringValue")
  190.             nameValue.Name = "ConveyorName"
  191.             nameValue.Value = selectedConveyor.Name
  192.             nameValue.Parent = conveyorModel
  193.         end
  194.        
  195.         conveyorModel.Name = "SELECTED CONVEYOR"
  196.         conveyorModel.Parent = cam
  197.        
  198.         local rayparams = RaycastParams.new()
  199.         rayparams.FilterDescendantsInstances = {conveyorModel, game.Players.LocalPlayer.Character}
  200.         local mouseRay = workspace:Raycast(cam.CFrame.Position, CFrame.new(cam.CFrame.Position, mouse.Hit.Position).LookVector * 100, rayparams)
  201.        
  202.         if mouseRay and (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - mouseRay.Position).Magnitude <= 30 then
  203.            
  204.             local pos = mouseRay.Position
  205.             pos = Vector3.new(math.round(pos.X * 2)/2, math.round(pos.Y * 2)/2, math.round(pos.Z * 2)/2)
  206.             pos = pos + Vector3.new(0, conveyorModel.Conveyor.Size.Y/2, 0)
  207.            
  208.             conveyorModel:PivotTo(CFrame.new(pos) * CFrame.Angles(0, math.rad(currentRotation), 0))
  209.            
  210.             local overlapparams = OverlapParams.new()
  211.             overlapparams.FilterType = Enum.RaycastFilterType.Whitelist
  212.             overlapparams.FilterDescendantsInstances = {workspace:WaitForChild("CREATED CONVEYORS")}
  213.             local partsInConveyor = workspace:GetPartBoundsInBox(conveyorModel.Conveyor.CFrame, conveyorModel.Conveyor.Size, overlapparams)
  214.            
  215.             local conveyorBlocked = false
  216.            
  217.             for _, part in pairs(partsInConveyor) do
  218.                 if part.Name == "Conveyor" then
  219.                     conveyorBlocked = true
  220.                     break
  221.                 end
  222.             end
  223.            
  224.             local raycastparams = RaycastParams.new()
  225.             raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
  226.             local downRay = workspace:Raycast(pos, Vector3.new(0, -100, 0), raycastparams)
  227.  
  228.             if not downRay or downRay.Instance:FindFirstAncestorWhichIsA("Folder") == workspace:WaitForChild("CREATED CONVEYORS") then
  229.                 conveyorBlocked = true
  230.             end
  231.            
  232.             for _, desc in pairs(conveyorModel:GetDescendants()) do
  233.                 if desc:IsA("BasePart") then
  234.                    
  235.                     if conveyorBlocked then
  236.                         desc.Color = Color3.new(math.clamp(desc.R.Value + 0.5, 0, 1), desc.G.Value, desc.B.Value)
  237.                     else
  238.                         desc.Color = Color3.new(desc.R.Value, math.clamp(desc.G.Value + 0.3, 0, 1), desc.B.Value)
  239.                     end
  240.                 end
  241.             end
  242.            
  243.         else
  244.             conveyorModel:Destroy()
  245.         end
  246.        
  247.     else
  248.        
  249.         if cam:FindFirstChild("SELECTED CONVEYOR") then
  250.             cam["SELECTED CONVEYOR"]:Destroy()
  251.         end
  252.     end
  253. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement