We use cookies on this site to enhance your user experience

Creating a Colorful Brick Tool

Creating a Colorful Brick Tool

Sep 07 2018, 4:34 AM PST

The code below creates a new tool and places into the Players/LocalPlayer's Player/Backpack. Then, the code equips the tool for the player user Humanoid/EquipTool.

When the tool is equipped and the player uses it (e.g. pressed their mouse button in-game), the code spawns a new BasePart|part at the position of the click using the Humanoid/TargetPoint position. The code also sets several of the part’s properties, such as giving it a random BasePart/BrickColor.

local player = game.Players.LocalPlayer
local character = player.CharacterAdded:wait()
local humanoid = character.Humanoid

-- Make a new tool and handle and put it in the player's Backpack
local function makeTool()
	-- Create tool
	local tool = Instance.new("Tool")
	tool.Parent = player:WaitForChild("Backpack")

	-- Create tool handle
	local handle = Instance.new("Part")
	handle.Name = "Handle"
	handle.Parent = tool
	handle.BrickColor = BrickColor.Random()
	
	-- Enable and equip tool
	tool.Enabled = true
	humanoid:EquipTool(tool)

	-- Handle tool use
	tool.Activated:Connect(onActivated)
end

-- Create a new randomly colored part at *pos* world position
local function spawnPart(pos)
	local part = Instance.new("Part")
	part.Anchored = true
	part.Size = Vector3.new(1,1,1)
	part.Position = pos
	part.Parent = game.Workspace
	part.BrickColor = BrickColor.Random()
end

-- Spawn a new part at TargetPoint when the tool is activated
function onActivated()
	spawnPart(humanoid.TargetPoint)
end

-- Make a new tool when the LocalScript first runs
makeTool()