Advertisement
killerbrenden

Placement System Source Code

Nov 23rd, 2020
2,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.88 KB | None | 0 0
  1. local replicatedStorage = game:GetService("ReplicatedStorage")
  2. local players = game:GetService("Players")
  3. local userInputService = game:GetService("UserInputService")
  4. local runService = game:GetService("RunService")
  5. local tweenService = game:GetService("TweenService")
  6.  
  7. local tweenInfo = TweenInfo.new(
  8.     0.25,
  9.     Enum.EasingStyle.Quad,
  10.     Enum.EasingDirection.Out,
  11.     0,
  12.     false,
  13.     0
  14. )
  15.  
  16. local gridInfo = TweenInfo.new(
  17.     0.5,
  18.     Enum.EasingStyle.Quad,
  19.     Enum.EasingDirection.Out,
  20.     0,
  21.     false,
  22.     0
  23. )
  24.  
  25. local player = players.LocalPlayer
  26. local mouse = player:GetMouse()
  27. local character = player.Character or player.CharacterAdded:Wait()
  28.  
  29. local canBuild = true
  30. local currentBuild
  31. local canPlace = false
  32. local isPlacing = false
  33.  
  34. local turnRight = false
  35. local turnLeft = false
  36. local newOr = 0
  37.  
  38. local gS = 3
  39. local tiles = {}
  40. local currentTile
  41.  
  42. local maxDist = 50
  43.  
  44. local aiming
  45. local selected
  46.  
  47. local furn = replicatedStorage:WaitForChild("Furniture")
  48. local remotes = replicatedStorage:WaitForChild("Remotes")
  49.  
  50. local interface = script.Parent:WaitForChild("Interface")
  51. local h = interface:WaitForChild("Holder")
  52. local sP = h:WaitForChild("SidePanel")
  53. local bV = h:WaitForChild("BuildView")
  54. local temps = h:WaitForChild("Templates")
  55.  
  56. local function createTile(vec3, n1, n2, parent)
  57.     local newTile = script:WaitForChild("Tile"):Clone()
  58.     newTile.Position = vec3
  59.     local nameTS = "Tile"..n1..n2
  60.     newTile.Name = nameTS
  61.     newTile.Highlight.Visible = true
  62.     newTile.Parent = parent
  63.     local tween = tweenService:Create(newTile, gridInfo, {Size = Vector3.new(gS,0.05,gS)})
  64.     tween:Play()
  65.     tiles[#tiles+1] = newTile
  66. end
  67.  
  68. local function createGrid(floor, grid)
  69.     local xs = floor.Size.X
  70.     local zs = floor.Size.Z
  71.    
  72.     local xp = floor.Position.X
  73.     local zp = floor.Position.Z
  74.     local yp = floor.Position.Y
  75.    
  76.     local gridSize
  77.    
  78.     if not grid then
  79.         gridSize = 3
  80.     else
  81.         gridSize = grid
  82.     end
  83.    
  84.     local xVals = {}
  85.     local zVals = {}
  86.    
  87.     for zVec = zs, gridSize, -gridSize do
  88.         zVals[#zVals+1] = zVec
  89.     end
  90.    
  91.     for xVec = xs, gridSize, -gridSize do
  92.         xVals[#xVals+1] = xVec
  93.     end
  94.    
  95.     for n,xv in pairs(xVals) do
  96.         for i,zv in pairs(zVals) do
  97.             local newVec = Vector3.new((xp - (xs / 2)) + xv - (gridSize / 2), (yp * 2) + 0.05, (zp - (zs / 2)) + zv - (gridSize / 2))
  98.             createTile(newVec, n, i, floor)
  99.         end
  100.     end
  101. end
  102.  
  103. local function removeGrid()
  104.     for num = 1,#tiles do
  105.         coroutine.wrap(function()
  106.             local tween = tweenService:Create(tiles[num],tweenInfo,{Size = Vector3.new(0.05,0.05,0.05)})
  107.             tween:Play()
  108.             tween.Completed:Wait()
  109.             tiles[num]:Destroy()
  110.             tiles[num]=nil
  111.         end)()
  112.     end
  113. end
  114.  
  115. sP.BuildMode.Activated:Connect(function()
  116.     if canBuild then
  117.         canBuild = false
  118.         sP.BuildMode.Text = "Build Mode: On"
  119.        
  120.         createGrid(workspace.Floor, gS)
  121.        
  122.         currentTile = nil
  123.        
  124.         for _,f in pairs(furn:GetChildren()) do
  125.             local d = temps.ItemTemplate:Clone()
  126.             d.Parent = bV.InventoryDisplay
  127.             d.Name = f.Name
  128.             d.Visible = true
  129.         end
  130.        
  131.         bV:TweenPosition(UDim2.new(0,0,1,0),"Out","Quad",1,true)
  132.        
  133.         local params = RaycastParams.new()
  134.         params.FilterType = Enum.RaycastFilterType.Blacklist
  135.         params.FilterDescendantsInstances = {character}
  136.        
  137.         coroutine.wrap(function()
  138.             local selectionBox = Instance.new("SelectionBox")
  139.             selectionBox.Parent = replicatedStorage
  140.             selectionBox.Color3 = Color3.fromRGB(0,255,0)
  141.             selectionBox.LineThickness = 0.05
  142.             selectionBox.Transparency = 0.5
  143.             selectionBox.SurfaceTransparency = 1
  144.             selectionBox.Name = "Selection"
  145.            
  146.             runService.RenderStepped:Connect(function()
  147.                 local params = RaycastParams.new()
  148.                 params.FilterType = Enum.RaycastFilterType.Blacklist
  149.                 params.FilterDescendantsInstances = {character}
  150.                
  151.                 local mouseRay = mouse.UnitRay
  152.                
  153.                 local nrR = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, params)
  154.                
  155.                 if nrR then
  156.                     if nrR.Instance and not canBuild and not isPlacing then
  157.                         if nrR.Instance:IsA("BasePart") and nrR.Instance.Name == "Hitbox" then
  158.                             local isOwner = remotes.GetOwner:InvokeServer(nrR.Instance)
  159.                             if isOwner then
  160.                                 aiming = nrR.Instance
  161.                             else
  162.                                 aiming = nil
  163.                             end
  164.                         else
  165.                             aiming = nil
  166.                         end
  167.                     end
  168.                 end
  169.                
  170.                 if aiming then
  171.                     selectionBox.Adornee = aiming
  172.                     selectionBox.Parent = aiming
  173.                 else
  174.                     selectionBox.Adornee = nil
  175.                     selectionBox.Parent = replicatedStorage
  176.                 end
  177.                
  178.                 userInputService.InputBegan:Connect(function(i, g)
  179.                     if g then return end
  180.                    
  181.                     if i.UserInputType == Enum.UserInputType.MouseButton1 then
  182.                         if aiming then
  183.                             selected = aiming.Parent
  184.                         end
  185.                     end
  186.                 end)
  187.                
  188.                 if selected then
  189.                     remotes:WaitForChild("PickupItem"):FireServer(selected)
  190.                     selected = nil
  191.                     aiming = nil
  192.                 end
  193.             end)
  194.         end)()
  195.        
  196.         for _,i in pairs(bV.InventoryDisplay:GetChildren()) do
  197.             coroutine.wrap(function()
  198.                 if i:IsA("ImageButton") then
  199.                     i.Activated:Connect(function()
  200.                         if not isPlacing then
  201.                             local toBuild = furn:FindFirstChild(i.Name):Clone()
  202.                            
  203.                             isPlacing = true
  204.                            
  205.                             local selectionBox = Instance.new("SelectionBox")
  206.                             selectionBox.Adornee = toBuild.PrimaryPart
  207.                             selectionBox.Color3 = Color3.fromRGB(0,255,0)
  208.                             selectionBox.LineThickness = 0.05
  209.                             selectionBox.Transparency = 0.5
  210.                             selectionBox.Parent = toBuild.PrimaryPart
  211.                             selectionBox.SurfaceTransparency = 1
  212.                             selectionBox.Name = "Selection"
  213.                            
  214.                             toBuild:SetPrimaryPartCFrame(CFrame.new(mouse.Hit.Position + Vector3.new(0,toBuild.PrimaryPart.Size.Y / 2, 0)))
  215.                            
  216.                             toBuild.Parent = workspace
  217.                             toBuild.PrimaryPart.Transparency = 0.75
  218.                            
  219.                             currentBuild = toBuild
  220.                            
  221.                             for _,part in pairs(toBuild:GetChildren()) do
  222.                                 part.CanCollide = false
  223.                             end
  224.                            
  225.                             coroutine.wrap(function()
  226.                                 while not canBuild do
  227.                                     pcall(function()
  228.                                         runService.RenderStepped:Wait()
  229.                                        
  230.                                         local pP = currentBuild.PrimaryPart
  231.                                         local mouseRay = mouse.UnitRay
  232.                                        
  233.                                         local params = RaycastParams.new()
  234.                                         params.FilterType = Enum.RaycastFilterType.Blacklist
  235.                                         params.FilterDescendantsInstances = {currentBuild, character}
  236.                                        
  237.                                         local rR = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, params)
  238.                                        
  239.                                         if (character:WaitForChild("HumanoidRootPart").Position - rR.Position).Magnitude <= maxDist then
  240.                                             if canPlace then
  241.                                                 pP.Color = Color3.fromRGB(0,255,0)
  242.                                                 selectionBox.Color3 = Color3.fromRGB(0,255,0)
  243.                                             else
  244.                                                 pP.Color = Color3.fromRGB(255,0,0)
  245.                                                 selectionBox.Color3 = Color3.fromRGB(255,0,0)
  246.                                             end
  247.                                            
  248.                                             if rR then
  249.                                                 if string.match(rR.Instance.Name:lower(), "tile", 1) or string.match(rR.Instance.Name:lower(), "floor", 1) or string.match(rR.Instance.Name:lower(), "highlight", 1) then
  250.                                                     currentTile = rR.Instance.Name
  251.                                                     local vec3 = Vector3.new(rR.Instance.Position.X, rR.Position.Y, rR.Instance.Position.Z)
  252.                                                     local offset = Vector3.new(0,pP.Size.Y / 2, 0)
  253.                                                     local rotation = CFrame.Angles(0,math.rad(newOr),0)
  254.                                                    
  255.                                                     local newPosCF = CFrame.new(vec3 + offset) * rotation
  256.                                                     local tween = tweenService:Create(pP, tweenInfo, {CFrame = newPosCF})
  257.                                                     tween:Play()
  258.                                                    
  259.                                                     local minVec = Vector3.new(pP.Position.X - (pP.Size.X / 2) + 0.1, pP.Position.Y - (pP.Size.Y / 2), pP.Position.Z - (pP.Size.Z / 2) + 0.1)
  260.                                                     local maxVec = Vector3.new(pP.Position.X + (pP.Size.X / 2) + 0.1, pP.Position.Y + (pP.Size.Y / 2), pP.Position.Z + (pP.Size.Z / 2) + 0.1)
  261.                                                    
  262.                                                     local region = Region3.new(minVec, maxVec)
  263.                                                    
  264.                                                     local inter = workspace:FindPartsInRegion3WithIgnoreList(region, {character, currentBuild, selectionBox})
  265.                                                    
  266.                                                     for _,p in pairs(inter) do
  267.                                                         if #inter <= 3 and not turnLeft and not turnRight and string.match(p.Name:lower(), "tile", 1) or string.match(p.Name:lower(), "floor", 1) or string.match(p.Name:lower(), "highlight", 1) then
  268.                                                             canPlace = true
  269.                                                         else
  270.                                                             canPlace = false
  271.                                                         end
  272.                                                     end
  273.                                                    
  274.                                                 elseif not string.match(rR.Instance.Name:lower(), "tile", 1) or string.match(rR.Instance.Name:lower(), "floor", 1) or string.match(rR.Instance.Name:lower(), "highlight", 1) then
  275.                                                     canPlace = false
  276.                                                 end
  277.                                             end
  278.                                         end
  279.                                     end)
  280.                                 end
  281.                             end)()
  282.                         end
  283.                     end)
  284.                 end
  285.             end)()
  286.         end
  287.        
  288.     elseif not canBuild then
  289.         canBuild = true
  290.         sP.BuildMode.Text = "Build Mode: Off"
  291.         if currentBuild then
  292.             currentBuild:Destroy()
  293.             currentBuild = nil
  294.         end
  295.         currentTile = nil
  296.         removeGrid()
  297.         isPlacing = false
  298.         newOr = 0
  299.         bV:TweenPosition(UDim2.new(0,0,2.25,0),"Out","Quad",1,true)
  300.         for _,e in pairs(bV.InventoryDisplay:GetChildren()) do
  301.             coroutine.wrap(function()
  302.                 if e.Name ~= "UIGridLayout" then
  303.                     e:Destroy()
  304.                 end
  305.             end)()
  306.         end
  307.     end
  308. end)
  309.  
  310. userInputService.InputBegan:Connect(function(input, gpe)
  311.     if gpe then return end
  312.    
  313.     if input.KeyCode == Enum.KeyCode.E and not canBuild and not turnRight and not turnLeft then
  314.         turnRight = true
  315.         newOr -= 90
  316.         wait(0.35)
  317.         turnRight = false
  318.     elseif input.KeyCode == Enum.KeyCode.Q and not canBuild and not turnRight and not turnLeft then
  319.         turnLeft = true
  320.         newOr += 90
  321.         wait(0.35)
  322.         turnLeft = false
  323.     elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not canBuild and canPlace then
  324.         local partCF = currentBuild:GetPrimaryPartCFrame()
  325.         remotes.PlaceItem:FireServer(currentBuild.Name, partCF, currentTile)
  326.         currentTile = nil
  327.         currentBuild:Destroy()
  328.         currentBuild = nil
  329.         canPlace = false
  330.         newOr = 0
  331.         isPlacing = false
  332.     end
  333. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement