Advertisement
HowToRoblox

PlacementHandler

Jun 2nd, 2021
1,873
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. local furnitureFolder = game.ReplicatedStorage:WaitForChild("Furniture")
  2. local re = game.ReplicatedStorage:WaitForChild("PlaceFurnitureRE")
  3.  
  4. local chosenFurniture = nil
  5.  
  6.  
  7. function handleFurniture(furniture)
  8.    
  9.     local btn = script:WaitForChild("FurnitureButton"):Clone()
  10.  
  11.     btn.FurnitureName.Text = furniture.Name
  12.  
  13.     local furnitureClone = furniture:Clone()
  14.     furnitureClone.Parent = btn.FurnitureImage
  15.  
  16.     local cam = Instance.new("Camera")
  17.     btn.FurnitureImage.CurrentCamera = cam
  18.     cam.Parent = btn.FurnitureImage
  19.  
  20.     cam.CFrame = CFrame.new(furnitureClone.PrimaryPart.Position + Vector3.new(5, 5, 5), furnitureClone.PrimaryPart.Position)
  21.  
  22.     btn.Parent = script.Parent
  23.  
  24.     btn.MouseButton1Click:Connect(function()
  25.         chosenFurniture = furniture
  26.     end)
  27. end
  28.  
  29.  
  30. for i, furniture in pairs(furnitureFolder:GetChildren()) do
  31.     handleFurniture(furniture)
  32. end
  33.  
  34. furnitureFolder.ChildAdded:Connect(handleFurniture)
  35.  
  36.  
  37.  
  38. local mouse = game.Players.LocalPlayer:GetMouse()
  39.  
  40.  
  41. local isPlacing = false
  42. mouse.Button1Down:Connect(function()
  43.     isPlacing = true
  44. end)
  45. mouse.Button1Up:Connect(function()
  46.     isPlacing = false
  47. end)
  48.  
  49.  
  50. local isRemoving = false
  51. mouse.Button2Down:Connect(function()
  52.     isRemoving = true
  53. end)
  54. mouse.Button2Up:Connect(function()
  55.     isRemoving = false
  56. end)
  57.  
  58.  
  59. local rotating = false
  60. local rotation = 0
  61.  
  62. game:GetService("UserInputService").InputBegan:Connect(function(inp)
  63.     if inp.KeyCode == Enum.KeyCode.R then
  64.         rotating = true
  65.     end
  66. end)
  67. game:GetService("UserInputService").InputEnded:Connect(function(inp)
  68.     if inp.KeyCode == Enum.KeyCode.R then
  69.         rotating = false
  70.     end
  71. end)
  72.  
  73.  
  74. local ghostItem = nil
  75.  
  76. while wait() do
  77.    
  78.     if ghostItem then ghostItem:Destroy() end
  79.    
  80.     local hit = mouse.Hit
  81.     local target = mouse.Target
  82.    
  83.     if rotating then rotation += 0.1 end
  84.    
  85.     if hit and chosenFurniture and mouse.Target and mouse.Target.Name == "Plot" then
  86.        
  87.         ghostItem = chosenFurniture:Clone()
  88.        
  89.         for i, d in pairs(ghostItem:GetDescendants()) do
  90.             if d:IsA("BasePart") then d.Transparency = 0.7; d.CanCollide = false end
  91.         end
  92.        
  93.         local ghostPos = hit.Position + Vector3.new(0, ghostItem.PrimaryPart.Size.Y/2, 0)
  94.         local ghostRot = ghostItem.PrimaryPart.CFrame - ghostItem.PrimaryPart.Position
  95.        
  96.        
  97.         ghostItem:SetPrimaryPartCFrame(CFrame.new(ghostPos) * ghostRot * CFrame.Angles(0, rotation, 0))
  98.        
  99.         ghostItem.Parent = workspace
  100.        
  101.        
  102.         if isPlacing then  
  103.             re:FireServer(chosenFurniture, ghostItem.PrimaryPart.CFrame, true, target)
  104.         end
  105.     end
  106.    
  107.     if isRemoving then
  108.         re:FireServer(mouse.Target)
  109.     end
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement