Guest User

yeet

a guest
Aug 27th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.54 KB | None | 0 0
  1.  
  2.  
  3.  
  4. local IncludeTextureForGrid = true
  5. local GridTexture = "rbxassetid://13786085"  
  6. local IncludeGrid = true
  7. local GridSize = 2
  8.  
  9.  
  10.  
  11. local Lerp = true  
  12. local LerpSpeed = 0.1
  13.  
  14.  
  15.  
  16. local RotIncrement = 90
  17. local FloorHeight = 1
  18. local MaxHeight = 60
  19.  
  20. local DetectCollision = true
  21. local IgnoreItems = false
  22. local BuildModePlacing = true
  23. local IncludeFloors = true
  24.  
  25.  
  26.  
  27. local CollisionColor = Color3.fromRGB(255, 55, 55) -- The Color of the model when colliding
  28. local NoCollisionColor = Color3.fromRGB(55, 255, 55) -- The Color of the model when not colliding
  29.  
  30.  
  31.  
  32. local RaiseButton = Enum.KeyCode.U
  33. local LowerButton = Enum.KeyCode.L
  34. local RotateButton = Enum.KeyCode.R
  35. local CancelButton = Enum.KeyCode.X
  36.  
  37.  
  38.  
  39. local placement = {}
  40.  
  41. local runService = game:GetService("RunService")
  42. local userInputService = game:GetService("UserInputService")
  43.  
  44. local player = game.Players.LocalPlayer
  45. local character = player.Character or player.CharacterAdded:Wait()
  46. local mouse = player:GetMouse()
  47.  
  48. wait(0.1) -- DO NOT REMOVE
  49.  
  50. local humanoid = character:FindFirstChild("Humanoid")
  51.  
  52. local posX
  53. local posY
  54. local posZ
  55. local startingY
  56. local rot = 0
  57.  
  58. local gridScale = math.abs(tonumber(GridSize))
  59.  
  60. local model
  61. local plot
  62. local ignorable
  63. local collisionPoints
  64. local collisionPoint
  65.  
  66. local isPlacing = nil
  67. local canPlace = nil
  68. local isColliding = nil
  69. local canStart = true
  70. local isStacking = nil
  71.  
  72. local lctn
  73.  
  74. local function SwitchVarsFalse()
  75.     isPlacing = false
  76.     canPlace = false
  77.     isColliding = false
  78.    
  79.     canStart = true
  80.    
  81.     if IgnoreItems then
  82.         mouse.TargetFilter = nil
  83.     end
  84. end
  85.  
  86. local function SwitchVarsTrue()
  87.     isPlacing = true
  88.     canPlace = true
  89.    
  90.     isColliding = false
  91.    
  92.     if BuildModePlacing then
  93.         canStart = true
  94.     else
  95.         canStart = false
  96.     end
  97. end
  98.  
  99. local function CalcMousePos()
  100.     if IncludeGrid then
  101.         posX = math.floor(mouse.Hit.X / gridScale + 0.5) * gridScale
  102.         posZ = math.floor(mouse.Hit.Z / gridScale + 0.5) * gridScale
  103.     else
  104.         posX = mouse.Hit.X
  105.         posZ = mouse.Hit.Z
  106.     end
  107.    
  108.     if IncludeFloors then
  109.         if isStacking then
  110.             posY = math.floor(mouse.Hit.Y) + 2
  111.         else
  112.             if posY > MaxHeight then
  113.                 posY = MaxHeight
  114.             elseif posY < startingY then
  115.                 posY = startingY
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121.  
  122. local function Rotate()
  123.     if Lerp then
  124.         CalcMousePos()
  125.    
  126.         rot = rot + RotIncrement
  127.            
  128.         model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:Lerp(CFrame.new(posX, posY, posZ) * CFrame.Angles(0, math.rad(rot), 0), LerpSpeed))
  129.     else
  130.         CalcMousePos()
  131.            
  132.         rot = rot + RotIncrement
  133.            
  134.         model:SetPrimaryPartCFrame(CFrame.new(posX, posY, posZ) * CFrame.Angles(0, math.rad(rot), 0))
  135.     end
  136. end
  137.  
  138. local function CollisionDetection(mdl)
  139.     if mdl and model then
  140.         isColliding = false
  141.        
  142.         collisionPoint =  mdl.PrimaryPart.Touched:Connect(function() end)
  143.         collisionPoints = mdl.PrimaryPart:GetTouchingParts()
  144.        
  145.         for i = 1, #collisionPoints do
  146.             if not collisionPoints[i]:IsDescendantOf(mdl) and not collisionPoints[i]:IsDescendantOf(character) then
  147.                 isColliding = true
  148.                
  149.                 break
  150.             end
  151.         end
  152.        
  153.         collisionPoint:Disconnect()
  154.         return isColliding
  155.     end
  156. end
  157.  
  158. local function HideGrid(plt)
  159.     if IncludeTextureForGrid then
  160.         for i, v in pairs(plt:GetChildren()) do
  161.             if v then
  162.                 v:Destroy()
  163.             end
  164.         end
  165.     end
  166. end
  167.  
  168. local function ShowGrid(plt)
  169.     HideGrid(plt)
  170.    
  171.     if IncludeTextureForGrid then
  172.         local GridTex = Instance.new("Texture")
  173.            
  174.         GridTex.Texture = GridTexture
  175.         GridTex.Face = Enum.NormalId.Top
  176.         GridTex.Transparency = 1
  177.         GridTex.Parent = plt
  178.    
  179.         GridTex.Transparency = 0
  180.     end
  181. end
  182.  
  183. local function OnDeath()
  184.     if isPlacing then
  185.         SwitchVarsFalse()
  186.         HideGrid(plot)
  187.        
  188.         if model then
  189.             model:Destroy()
  190.            
  191.             model = nil
  192.         end
  193.     end
  194. end
  195.  
  196. local function ChangeColor(mdl)
  197.     if isColliding and mdl and isPlacing then
  198.         mdl.PrimaryPart.Color = CollisionColor
  199.     else
  200.         mdl.PrimaryPart.Color = NoCollisionColor
  201.     end
  202. end
  203.  
  204. local function Inputs(input, gpe)
  205.     if input.KeyCode == RotateButton and isPlacing then
  206.         Rotate()
  207.     elseif input.KeyCode == CancelButton and model and isPlacing then
  208.         SwitchVarsFalse()
  209.        
  210.         model:Destroy()
  211.        
  212.         HideGrid(plot)
  213.     elseif input.KeyCode == RaiseButton and model and isPlacing and posY < MaxHeight then
  214.         posY = model.PrimaryPart.CFrame.Y + FloorHeight
  215.     elseif input.KeyCode == LowerButton and model and isPlacing and posY > startingY then
  216.         posY = model.PrimaryPart.CFrame.Y - FloorHeight
  217.     elseif input.KeyCode == Enum.KeyCode.ButtonB then
  218.         if isPlacing then
  219.             SwitchVarsFalse()
  220.            
  221.             model:Destroy()
  222.            
  223.             HideGrid(plot)
  224.         end
  225.     end
  226. end
  227.  
  228. local function MoveModelToCalculatedPos()
  229.     if isPlacing and model then
  230.         if IgnoreItems then
  231.             mouse.TargetFilter = ignorable
  232.         else
  233.             mouse.TargetFilter = model
  234.         end
  235.        
  236.         if Lerp then
  237.             CalcMousePos()
  238.            
  239.             if DetectCollision then
  240.                 CollisionDetection(model)
  241.                 ChangeColor(model)
  242.             else
  243.                 isColliding = false
  244.             end
  245.            
  246.             model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:Lerp(CFrame.new(posX, posY, posZ) * CFrame.Angles(0, math.rad(rot), 0), LerpSpeed))
  247.         else
  248.             CalcMousePos()
  249.            
  250.             if DetectCollision then
  251.                 CollisionDetection(model)
  252.                 ChangeColor(model)
  253.             end
  254.            
  255.             model:SetPrimaryPartCFrame(CFrame.new(posX, posY, posZ) * CFrame.Angles(0, math.rad(rot), 0))
  256.         end
  257.     end
  258. end
  259.  
  260. local function SetUpModel(modl)
  261.     for i, mdl in pairs(model:GetDescendants()) do
  262.         if mdl then
  263.             if mdl:IsA("BasePart") or mdl:IsA("UnionOperation") then
  264.                 mdl.CanCollide = false
  265.                 mdl.Anchored = true
  266.             end
  267.         end
  268.     end
  269.    
  270.     model.PrimaryPart.Transparency = 0.8
  271.    
  272.     if not DetectCollision then
  273.         model.PrimaryPart.Color = NoCollisionColor
  274.     end
  275.    
  276.     posY = model.PrimaryPart.CFrame.Y
  277.     startingY = model.PrimaryPart.CFrame.Y
  278.    
  279.     model.Parent = ignorable
  280. end
  281.  
  282. local function SwitchModel(mdl, location)
  283.     if model then
  284.         model:Destroy()
  285.        
  286.         model = location:FindFirstChild(mdl):Clone()
  287.        
  288.         SetUpModel(model)
  289.        
  290.         model:SetPrimaryPartCFrame(CFrame.new(posX, posY, posZ))
  291.     else
  292.         model = location:FindFirstChild(mdl):Clone()
  293.        
  294.         SetUpModel(model)
  295.        
  296.         model:SetPrimaryPartCFrame(CFrame.new(posX, posY, posZ))
  297.     end
  298. end
  299.  
  300. function placement:PlaceModel(location, RE)
  301.     if isPlacing and canPlace and not isColliding and model and not BuildModePlacing then
  302.         wait(0.2)
  303.        
  304.         CollisionDetection(model)
  305.        
  306.         CalcMousePos()
  307.        
  308.         RE:FireServer(model.Name, posX, posY, posZ, rot, location)
  309.        
  310.         SwitchVarsFalse()
  311.         HideGrid(plot)
  312.        
  313.         model:Destroy()
  314.     elseif not isColliding and canPlace and isPlacing then
  315.         CalcMousePos()
  316.        
  317.         RE:FireServer(model.Name, posX, posY, posZ, rot, location)
  318.        
  319.         SwitchModel(model.Name, lctn)
  320.         SwitchVarsTrue()
  321.        
  322.         wait(0.1)
  323.     end
  324. end
  325.  
  326. function placement:StartPlacement(location, mdlNm, plt, mdls, isSt)
  327.     if canStart and not isPlacing and not BuildModePlacing then
  328.         plot = plt
  329.         ignorable = mdls
  330.        
  331.         ShowGrid(plot)
  332.         SwitchVarsTrue()
  333.        
  334.         lctn = location
  335.         model = location:FindFirstChild(mdlNm):Clone()
  336.        
  337.         SetUpModel(model)
  338.        
  339.         if isSt then
  340.             IgnoreItems = false
  341.            
  342.             isStacking = true
  343.         else           
  344.             isStacking = false
  345.         end
  346.     elseif BuildModePlacing then
  347.         lctn = location
  348.         plot = plt
  349.         ignorable = mdls
  350.        
  351.         SwitchModel(mdlNm, location)
  352.         SwitchVarsTrue()
  353.         ShowGrid(plot)
  354.        
  355.         if isSt then
  356.             IgnoreItems = false
  357.            
  358.             isStacking = true
  359.         else
  360.             isStacking = false
  361.         end
  362.     end
  363. end
  364.  
  365. userInputService.InputBegan:Connect(Inputs)
  366. runService:BindToRenderStep("Input", Enum.RenderPriority.Input.Value, MoveModelToCalculatedPos)
  367.  
  368. humanoid.Died:Connect(OnDeath)
  369.  
  370. return placement
Advertisement
Add Comment
Please, Sign In to add comment