Advertisement
HowToRoblox

ConveyorSystemServer

Jan 26th, 2023 (edited)
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. local conveyors = game:GetService("ReplicatedStorage"):WaitForChild("ConveyorSystemReplicatedStorage"):WaitForChild("Conveyors")
  2. local placeRemote = game:GetService("ReplicatedStorage")["ConveyorSystemReplicatedStorage"]:WaitForChild("PlaceConveyorEvent")
  3. local removeRemote = game:GetService("ReplicatedStorage")["ConveyorSystemReplicatedStorage"]:WaitForChild("RemoveConveyorEvent")
  4.  
  5. local createdConveyors = Instance.new("Folder")
  6. createdConveyors.Name = "CREATED CONVEYORS"
  7. createdConveyors.Parent = workspace
  8.  
  9.  
  10. function hideWall(wall)
  11.    
  12.     wall.Transparency = 1
  13.     wall.CanCollide = false
  14.    
  15.     for _, desc in pairs(wall:GetDescendants()) do
  16.         if desc:IsA("BasePart") then
  17.             desc.Transparency = 1
  18.             desc.CanCollide = false
  19.         end
  20.     end
  21. end
  22.  
  23. function showWall(wall)
  24.     print(wall)
  25.     wall.Transparency = 0
  26.     wall.CanCollide = true
  27.  
  28.     for _, desc in pairs(wall:GetDescendants()) do
  29.         if desc:IsA("BasePart") then
  30.             desc.Transparency = 0
  31.             desc.CanCollide = true
  32.         end
  33.     end
  34. end
  35.  
  36.  
  37. placeRemote.OnServerEvent:Connect(function(plr, conveyor, pos, rot)
  38.    
  39.     if plr.Character and plr.Character.Humanoid.Health > 0 and conveyor and conveyor.Parent == conveyors then
  40.        
  41.         local dist = (plr.Character.HumanoidRootPart.Position - pos).Magnitude
  42.        
  43.         if dist <= 30 then
  44.             pos = Vector3.new(math.round(pos.X * 2)/2, math.round(pos.Y * 2)/2, math.round(pos.Z * 2)/2)
  45.             rot = math.round(rot / 90) * 90
  46.            
  47.             local raycastparams = RaycastParams.new()
  48.             raycastparams.FilterDescendantsInstances = {plr.Character}
  49.             local downRay = workspace:Raycast(pos, Vector3.new(0, -100, 0), raycastparams)
  50.            
  51.             if downRay and downRay.Instance:FindFirstAncestorWhichIsA("Folder") ~= createdConveyors then
  52.                 pos = Vector3.new(pos.X, downRay.Position.Y + conveyor.Conveyor.Size.Y/2, pos.Z)
  53.            
  54.                 local conveyorCF = CFrame.new(pos) * CFrame.Angles(0, math.rad(rot), 0)
  55.                
  56.                 local overlapparams = OverlapParams.new()
  57.                 overlapparams.FilterType = Enum.RaycastFilterType.Whitelist
  58.                 overlapparams.FilterDescendantsInstances = {createdConveyors}
  59.                 local partsInConveyor = workspace:GetPartBoundsInBox(conveyorCF, conveyor.Conveyor.Size, overlapparams)
  60.                
  61.                 local conveyorBlocked = false
  62.  
  63.                 for _, part in pairs(partsInConveyor) do
  64.                     if part.Name == "Conveyor" then
  65.                         conveyorBlocked = true
  66.                         break
  67.                     end
  68.                 end
  69.                
  70.                 if not conveyorBlocked then
  71.                    
  72.                     local newConveyor = conveyor:Clone()
  73.                     newConveyor:PivotTo(conveyorCF)
  74.                     newConveyor.Parent = createdConveyors
  75.                     newConveyor.Conveyor.AssemblyLinearVelocity = conveyorCF.LookVector * newConveyor.Configuration.Speed.Value
  76.                    
  77.                     local creatorValue = Instance.new("IntValue")
  78.                     creatorValue.Name = "CREATOR"
  79.                     creatorValue.Value = plr.UserId
  80.                     creatorValue.Parent = newConveyor
  81.                    
  82.                     local connectedValue = Instance.new("ObjectValue")
  83.                     connectedValue.Name = "CONNECTED TO"
  84.                     connectedValue.Parent = newConveyor
  85.                    
  86.                     hideWall(newConveyor.Walls.FrontWall)
  87.                    
  88.                     local raycastparams = RaycastParams.new()
  89.                     raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
  90.                    
  91.                     local conveyorWalls = {}
  92.                     for _, createdConveyor in pairs(createdConveyors:GetChildren()) do
  93.                         if createdConveyor ~= newConveyor then
  94.                             table.insert(conveyorWalls, createdConveyor.Walls)
  95.                         end
  96.                     end
  97.                     raycastparams.FilterDescendantsInstances = {conveyorWalls}
  98.                    
  99.                     local frontPos = newConveyor.Conveyor.Position
  100.                     frontPos = frontPos + newConveyor.Conveyor.Size.Z/2 * newConveyor.Conveyor.CFrame.LookVector
  101.                     frontPos = frontPos - newConveyor.Conveyor.CFrame.LookVector * 0.1
  102.                     local frontRay = workspace:Raycast(frontPos, newConveyor.Conveyor.CFrame.LookVector * 0.5, raycastparams)
  103.                    
  104.                     local backPos = newConveyor.Conveyor.Position
  105.                     backPos = backPos - newConveyor.Conveyor.Size.Z/2 * newConveyor.Conveyor.CFrame.LookVector
  106.                     backPos = backPos + newConveyor.Conveyor.CFrame.LookVector * 0.1
  107.                     local backRay = workspace:Raycast(backPos, -newConveyor.Conveyor.CFrame.LookVector * 0.5, raycastparams)
  108.                    
  109.                     local leftPos = newConveyor.Conveyor.Position
  110.                     leftPos = leftPos - newConveyor.Conveyor.Size.X/2 * newConveyor.Conveyor.CFrame.RightVector
  111.                     leftPos = leftPos + newConveyor.Conveyor.CFrame.RightVector * 0.1
  112.                     local leftRay = workspace:Raycast(leftPos, -newConveyor.Conveyor.CFrame.RightVector * 0.5, raycastparams)
  113.                    
  114.                     local rightPos = newConveyor.Conveyor.Position
  115.                     rightPos = rightPos + newConveyor.Conveyor.Size.X/2 * newConveyor.Conveyor.CFrame.RightVector
  116.                     rightPos = rightPos - newConveyor.Conveyor.CFrame.RightVector * 0.1
  117.                     local rightRay = workspace:Raycast(rightPos, newConveyor.Conveyor.CFrame.RightVector * 0.5, raycastparams)
  118.                    
  119.                     if frontRay then
  120.                        
  121.                         local hitWall = frontRay.Instance
  122.                         while hitWall.Parent.Name ~= "Walls" do
  123.                             hitWall = hitWall.Parent
  124.                         end
  125.  
  126.                         hideWall(hitWall)
  127.                         hideWall(newConveyor.Walls.FrontWall)
  128.                                
  129.                         connectedValue.Value = hitWall
  130.                        
  131.                         if hitWall.Name == "LeftWall" then
  132.                             local newVel = hitWall.Parent.Parent.Conveyor.CFrame.LookVector * math.sqrt((hitWall.Parent.Parent.Configuration.Speed.Value^2)/2)
  133.                             newVel = newVel + hitWall.Parent.Parent.Conveyor.CFrame.RightVector * math.sqrt((hitWall.Parent.Parent.Configuration.Speed.Value^2)/2)
  134.                             hitWall.Parent.Parent.Conveyor.AssemblyLinearVelocity = newVel
  135.                         elseif hitWall.Name == "RightWall" then
  136.                             local newVel = hitWall.Parent.Parent.Conveyor.CFrame.LookVector * math.sqrt((hitWall.Parent.Parent.Configuration.Speed.Value^2)/2)
  137.                             newVel = newVel - hitWall.Parent.Parent.Conveyor.CFrame.RightVector * math.sqrt((hitWall.Parent.Parent.Configuration.Speed.Value^2)/2)
  138.                             hitWall.Parent.Parent.Conveyor.AssemblyLinearVelocity = newVel
  139.                         end
  140.                     end
  141.                    
  142.                     if backRay then
  143.                        
  144.                         local hitWall = backRay.Instance
  145.                         while hitWall.Parent.Name ~= "Walls" do
  146.                             hitWall = hitWall.Parent
  147.                         end
  148.  
  149.                         if hitWall.Name == "FrontWall" and (not hitWall.Parent.Parent["CONNECTED TO"].Value or hitWall.Parent.Parent["CONNECTED TO"].Value.Parent == nil) then
  150.  
  151.                             hideWall(hitWall)
  152.                             hideWall(newConveyor.Walls.BackWall)
  153.  
  154.                             hitWall.Parent.Parent["CONNECTED TO"].Value = newConveyor.Walls.BackWall
  155.                         end
  156.                     end
  157.                    
  158.                     if leftRay then
  159.  
  160.                         local hitWall = leftRay.Instance
  161.                         while hitWall.Parent.Name ~= "Walls" do
  162.                             hitWall = hitWall.Parent
  163.                         end
  164.  
  165.                         if hitWall.Name == "FrontWall" and (not hitWall.Parent.Parent["CONNECTED TO"].Value or hitWall.Parent.Parent["CONNECTED TO"].Value.Parent == nil) then
  166.  
  167.                             hideWall(hitWall)
  168.                             hideWall(newConveyor.Walls.LeftWall)
  169.  
  170.                             hitWall.Parent.Parent["CONNECTED TO"].Value = newConveyor.Walls.LeftWall
  171.  
  172.                             local newVel = newConveyor.Conveyor.CFrame.LookVector * math.sqrt((newConveyor.Configuration.Speed.Value^2)/2)
  173.                             newVel = newVel + newConveyor.Conveyor.CFrame.RightVector * math.sqrt((newConveyor.Configuration.Speed.Value^2)/2)
  174.                             newConveyor.Conveyor.AssemblyLinearVelocity = newVel
  175.                         end
  176.                     end
  177.                        
  178.                     if rightRay then
  179.                        
  180.                         local hitWall = rightRay.Instance
  181.                         while hitWall.Parent.Name ~= "Walls" do
  182.                             hitWall = hitWall.Parent
  183.                         end
  184.  
  185.                         if hitWall.Name == "FrontWall" and (not hitWall.Parent.Parent["CONNECTED TO"].Value or hitWall.Parent.Parent["CONNECTED TO"].Value.Parent == nil) then
  186.  
  187.                             hideWall(hitWall)
  188.                             hideWall(newConveyor.Walls.RightWall)
  189.  
  190.                             hitWall.Parent.Parent["CONNECTED TO"].Value = newConveyor.Walls.RightWall
  191.  
  192.                             local newVel = newConveyor.Conveyor.CFrame.LookVector * math.sqrt((newConveyor.Configuration.Speed.Value^2)/2)
  193.                             newVel = newVel - newConveyor.Conveyor.CFrame.RightVector * math.sqrt((newConveyor.Configuration.Speed.Value^2)/2)
  194.                             newConveyor.Conveyor.AssemblyLinearVelocity = newVel
  195.                         end
  196.                     end
  197.                 end
  198.             end
  199.         end
  200.     end
  201. end)
  202.  
  203.  
  204. removeRemote.OnServerEvent:Connect(function(plr, conveyor)
  205.    
  206.     if plr and conveyor and conveyor.Parent == createdConveyors then
  207.        
  208.         if conveyor.CREATOR.Value == plr.UserId then
  209.            
  210.             if conveyor["CONNECTED TO"].Value and conveyor["CONNECTED TO"].Value.Parent ~= nil then
  211.                 showWall(conveyor["CONNECTED TO"].Value)
  212.                 conveyor["CONNECTED TO"].Value.Parent.Parent.Conveyor.AssemblyLinearVelocity = conveyor["CONNECTED TO"].Value.Parent.Parent.Conveyor.CFrame.LookVector * conveyor["CONNECTED TO"].Value.Parent.Parent.Configuration.Speed.Value
  213.             end
  214.            
  215.             conveyor:Destroy()
  216.         end
  217.     end
  218. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement