The Activated event fires when the player clicks while a
Tool is equipped.
This function is used to perform an action when the player uses the tool. For instance, when the player clicks while a Rocket Launcher tool is equipped, the activated event executes the code to create and launch a rocket.
The below code, when placed in a
LocalScript, would create a tool in the
Backpack. It will print “Tool activated” when the player clicks while the created tool is equipped.
local tool = Instance.new("Tool") tool.RequiresHandle = false tool.Parent = game.Players.LocalPlayer.Backpack function onActivation() print("Tool activated") end tool.Activated:Connect(onActivation)
Creating a Colorful Brick Tool
The code below creates a new tool and places into the
Player/Backpack. Then, the code equips the tool for the player user
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
Player Fly Tool
The code below demonstrates the functionality of several
Tool events, including
The code, when placed in a
LocalScript in the
StarterCharacterScripts folder inserts and controls a tool in the
Backpack that allows the player to fly through the sky when used.
In order to ensure that the player stops flying when the player unequips the tool, the code uses the tool’s Unequipped event to turn set the
fly boolean to true and turn stop flying until the tool is reequipped and activated again.