Advertisement
SkidScripts

Roblox Code for a Building Game (Lua)

Nov 29th, 2023 (edited)
975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. -- Function to create a block when the tool is activated
  2. local function createBuildingTool()
  3.     local tool = Instance.new("Tool")
  4.     tool.Name = "BuildingTool"
  5.    
  6.     local handle = Instance.new("Part")
  7.     handle.Name = "Handle"
  8.     handle.Size = Vector3.new(4, 1, 4)
  9.     handle.Anchored = true
  10.     handle.CanCollide = false
  11.     handle.BrickColor = BrickColor.new("Bright blue")
  12.     handle.Transparency = 0.5
  13.     handle.Parent = tool
  14.    
  15.     -- Function to place a block when the tool is activated by clicking
  16.     tool.Activated:Connect(function()
  17.         local character = tool.Parent
  18.         local humanoid = character:FindFirstChild("Humanoid")
  19.         if humanoid then
  20.             local mouse = humanoid:GetMouse()
  21.             local hit, position = workspace:FindPartOnRayWithIgnoreList(
  22.                 Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100),
  23.                 {character, tool}
  24.             )
  25.             if hit and hit:IsA("BasePart") then
  26.                 local newBlock = handle:Clone()
  27.                 newBlock.Position = position
  28.                 newBlock.Parent = workspace
  29.             end
  30.         end
  31.     end)
  32.    
  33.     -- Function to remove placed blocks when right-clicked
  34.     tool.Equipped:Connect(function()
  35.         local character = tool.Parent
  36.         local humanoid = character:FindFirstChild("Humanoid")
  37.         if humanoid then
  38.             local mouse = humanoid:GetMouse()
  39.             mouse.Button2Down:Connect(function()
  40.                 local hit, position = workspace:FindPartOnRayWithIgnoreList(
  41.                     Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100),
  42.                     {character, tool}
  43.                 )
  44.                 if hit and hit:IsA("Part") then
  45.                     hit:Destroy()
  46.                 end
  47.             end)
  48.         end
  49.     end)
  50.    
  51.     -- Function to drop the tool when unequipped
  52.     tool.Unequipped:Connect(function()
  53.         tool.Parent = game.Players.LocalPlayer.Character
  54.     end)
  55.    
  56.     tool.Parent = game.Players.LocalPlayer.Backpack
  57. end
  58.  
  59. -- Call the createBuildingTool function
  60. createBuildingTool()
Tags: Roblox lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement