HowToRoblox

PlacementHandler

Jan 18th, 2021 (edited)
2,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local snap = 3
  2.  
  3.  
  4. local blocks = game.ReplicatedStorage:WaitForChild("Blocks")
  5.  
  6. local placeRE = game.ReplicatedStorage:WaitForChild("PlaceBlockRE")
  7.  
  8.  
  9. local chosenBlock = blocks:GetChildren()[1]
  10.  
  11.  
  12. for i, block in pairs(blocks:GetChildren()) do
  13.    
  14.     local clonedTemplate = script:WaitForChild("BlockButton"):Clone()
  15.    
  16.     local name = block.Name
  17.     clonedTemplate.BlockName.Text = name
  18.    
  19.    
  20.     local blockClone = block:Clone()
  21.     blockClone.Size = Vector3.new(snap, snap, snap)
  22.     blockClone.Parent = clonedTemplate.BlockImage
  23.    
  24.    
  25.     local cam = Instance.new("Camera")
  26.     clonedTemplate.BlockImage.CurrentCamera = cam
  27.     cam.Parent = clonedTemplate.BlockImage
  28.  
  29.     cam.CFrame = CFrame.new(blockClone.Position + Vector3.new(3.3, 3.3, 3.3), blockClone.Position)
  30.    
  31.    
  32.     clonedTemplate.Parent = script.Parent
  33.    
  34.    
  35.     clonedTemplate.MouseButton1Click:Connect(function()
  36.         chosenBlock = blocks[name]
  37.     end)
  38. end
  39.  
  40.  
  41.  
  42. local mouse = game.Players.LocalPlayer:GetMouse()
  43.  
  44. local selectingBox = Instance.new("SelectionBox", workspace)
  45.  
  46. game:GetService("RunService").RenderStepped:Connect(function()
  47.    
  48.     local target = mouse.Target
  49.     if target then
  50.        
  51.         if target:FindFirstChild("Placeable") then
  52.            
  53.             selectingBox.Adornee = target
  54.            
  55.         else
  56.             selectingBox.Adornee = nil
  57.         end
  58.     end
  59. end)
  60.  
  61.  
  62. local isRemoving = false
  63.  
  64. mouse.Button1Down:Connect(function()
  65.     isRemoving = true
  66. end)
  67. mouse.Button1Up:Connect(function()
  68.     isRemoving = false
  69. end)
  70.  
  71.  
  72. local isPlacing = false
  73.  
  74. mouse.Button2Down:Connect(function()
  75.     isPlacing = true
  76. end)
  77. mouse.Button2Up:Connect(function()
  78.     isPlacing = false
  79. end)
  80.  
  81.  
  82. game.ReplicatedStorage.PlaySoundRE.OnClientEvent:Connect(function()
  83.     script.Sound:Play()
  84. end)
  85.  
  86.  
  87. local blockCooldown = 0.08
  88.  
  89. while wait() do
  90.    
  91.     local target = mouse.Target
  92.     if target and target:FindFirstChild("Placeable") then
  93.    
  94.         if isPlacing then
  95.                        
  96.             placeRE:FireServer(chosenBlock, target, mouse.TargetSurface, "placing")
  97.             wait(blockCooldown)    
  98.            
  99.         elseif isRemoving then
  100.                
  101.             placeRE:FireServer(nil, target, nil, "removing")
  102.             wait(blockCooldown)
  103.         end
  104.     end
  105. end
Add Comment
Please, Sign In to add comment