Tool

Show Deprecated

Tools are objects that a Humanoid object can equip. For players, they are stored in a Backpack object parented to a Player object. In-game, players may have multiple tools which appear as icons at the bottom of the screen. Equipping a tool moves it from the Backpack and into a Player.Character model in the Workspace. By default, tools are held in the right hand and have a handle in them, which is a Part named "Handle" inside (though one isn't required if Tool.RequiresHandle is off). Tools that are to be provided to (re)spawning players ought to be stored in the StarterPack.

On desktop, pressing a number key (1, 2, 3...) will equip a tool. Equipped tools can be dropped into the Workspace by pressing Backspace. It's recommended that you turn Tool.CanBeDropped off so it isn't possible to drop a tool, die, respawn and drop again to duplicate tools. On gamepads, LB and RB buttons will equip tools. You can disable activation via left click (or right trigger on gamepad) by setting Tool.ManualActivationOnly on. Doing so requires that you call Activate yourself through some sort of other user input.

Tools aren't the only way to capture user input. You can also use ContextActionService, UserInputService or Player:GetMouse(). If you need a Tool to have multiple actions, such as pressing a key while the Tool is equipped, you should use ContextActionService's BindAction and UnbindAction in the Equipped and Unequipped events, respectively. Use a LocalScript send these actions to the server via a RemoteFunction inside the Tool.

Code Samples

Explode Tool Example

local tool = script.Parent
local function explode(point)
local e = Instance.new("Explosion")
e.DestroyJointRadiusPercent = 0 -- Make the explosion non-deadly
e.Position = point
e.Parent = workspace
end
local function onActivated()
-- Get the Humanoid that Activated the tool
local human = tool.Parent.Humanoid
-- Call explode with the current point the Humanoid is targetting
explode(human.TargetPoint)
end
tool.Activated:Connect(onActivated)
Sword Tool Example

local tool = script.Parent
local function onTouch(partOther)
-- First, try to see if the part we touched was part of a Humanoid
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
-- Ignore touches by non-humanoids
if not humanOther then
return
end
-- Ignore touches by the Humanoid carrying the sword
if humanOther.Parent == tool.Parent then
return
end
humanOther:TakeDamage(5)
end
-- Trigger a slash animation
local function slash()
-- Default character scripts will listen for a "toolanim" StringValue
local value = Instance.new("StringValue")
value.Name = "toolanim"
value.Value = "Slash" -- try also: Lunge
value.Parent = tool
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)

Summary

Properties

  • read parallel

    Controls whether the player can drop the tool.

  • read parallel

    Relates to whether or not the tool can be used.

  • read parallel

    Stores the tool's "grip" properties as one CFrame.

  • hidden
    not replicated
    read parallel

    One of the properties that specifies a Tool's orientation in a character's hand. This represents the R02, R12, and R22 values of the Grip CFrame's rotation matrix.

  • hidden
    not replicated
    read parallel

    The positional offset of a Tool weld matrix.

  • hidden
    not replicated
    read parallel

    One of the properties that specifies a Tool's orientation in a character's hand. This represents the R00, R10, and R20 values of the Grip CFrame's rotation matrix.

  • hidden
    not replicated
    read parallel

    One of the properties that specifies a Tool's orientation in a character's hand. This represents the R01, R11, and R21 values of the Grip CFrame's rotation matrix.

  • The ManualActivationOnly property controls whether the Tool can be activated without executing Tool:Activate().

  • read parallel

    Determines whether a Tool functions without a handle.

  • read parallel

    Controls the message displayed when the player's mouse hovers over the tool in their backpack.

Properties inherited from BackpackItem
  • read parallel

    The texture icon that is displayed for a tool in the player's backpack.

Properties inherited from ModelProperties inherited from PVInstance

Methods

  • Activate():void

    Simulates a click on a Tool.

  • Deactivate():void

    Simulates the deactivation of a Tool.

Methods inherited from Model
  • AddPersistentPlayer(playerInstance: Player):void

    Sets this model to be persistent for the specified player. Model.ModelStreamingMode must be set to PersistentPerPlayer for behavior to be changed as a result of addition.

  • Returns a description of a volume that contains all parts of a Model.

  • Returns the size of the smallest bounding box that contains all of the BaseParts in the Model, aligned with the Model.PrimaryPart if it is set.

  • Returns all the Player objects that this model object is persistent for. Behavior varies based on whether this method is called from a Script or a LocalScript.

  • Returns the canonical scale of the model, which defaults to 1 for newly created models and will change as it is scaled via Model/ScaleTo.

  • MoveTo(position: Vector3):void

    Moves the PrimaryPart to the given position. If a primary part has not been specified, the root part of the model will be used.

  • RemovePersistentPlayer(playerInstance: Player):void

    Makes this model no longer persistent for specified player. Model.ModelStreamingMode must be set to PersistentPerPlayer for behavior to be changed as a result of removal.

  • ScaleTo(newScaleFactor: number):void

    Sets the scale factor of the model, adjusting the sizing and location of all descendant Instances such that they have that scale factor relative to their initial sizes and locations when scale factor was 1.

  • TranslateBy(delta: Vector3):void

    Shifts a Model by the given Vector3 offset, preserving the model's orientation. If another BasePart or Terrain already exists at the new position then the Model will overlap said object.

Methods inherited from PVInstance

Events

Properties

CanBeDropped

read parallel

The CanBeDropped property controls whether the player can drop the Tool.

If true, when the backspace button is pressed, the tool will be parented to Workspace and removed from the player's Backpack. If false, nothing will happen when backspace is pressed, and the tool will remain equipped.

Code Samples

Meow Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local tool = Instance.new("Tool")
tool.Name = "I AM CAT"
tool.RequiresHandle = false
tool.CanBeDropped = false
tool.Parent = backpack
local function onToolActivated()
print("Meow")
end
tool.Activated:Connect(onToolActivated)

Enabled

read parallel

The Enabled property relates to whether or not the Tool can be used. This is useful if you want to prevent a player from using a tool, but don't want to remove it from their Backpack.

When set to true, the player can use the tool. When set to false, the tool is disabled and the player cannot use it; this prevents the tool from being activated or deactivated by the Tool:Activate() and Tool:Deactivate() methods, and it prevents the Tool.Activated and Tool.Deactivated events from firing.

Code Samples

Superjump Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = Instance.new("Tool")
tool.Name = "SuperJump"
tool.RequiresHandle = false
tool.Parent = player.Backpack
function toolActivated()
humanoid.JumpPower = 150
tool.Enabled = false
task.wait(5)
tool.Enabled = true
humanoid.JumpPower = 50
end
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(function()
humanoid.JumpPower = 50
end)

Grip

read parallel

The Grip property stores the tool's "grip" properties as a single CFrame. These properties position how the player holds the tool and include GripUp, GripRight, GripForward, and GripPos.

Unlike the grip properties that it stores, this consolidated property doesn't appear in the Properties window in Studio. Regardless, you can set and get it from a Script or LocalScript.

Code Samples

Grip Stick

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)

GripForward

hidden
not replicated
read parallel

The GripForward properties is one of the properties that specifies a Tool's orientation in a character's hand. This represents the R02, R12, and R22 values of the Grip CFrame's rotation matrix.

Other tool properties that control how a player holds a tool include: Up, Right, and Pos properties. All of these properties are stored in a single CFrame in the Tool.Grip property.

In order to change a tool's grip properties, you must either use a Script or LocalScript or a Studio plugin such as the Tool Grip Editor example.

Code Samples

Grip Stick

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)

GripPos

hidden
not replicated
read parallel

The GripPos property controls the positional offset of a Tool weld matrix. It is one of several properties used to position how the player holds the tool.

Other tool properties that control how a player holds a tool include: Up, Right, and Forward properties. All of these properties are stored in a single CFrame in the Tool.Grip property.

In order to change a tool's grip properties, you must either use a Script or LocalScript or a plugin such as the Tool Grip Editor example.

Code Samples

Grip Stick

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)

GripRight

hidden
not replicated
read parallel

The GripRight property is one of the properties that specifies a Tool's orientation in a character's hand. This represents the R00, R10, and R20 values of the Grip CFrame's rotation matrix.

Other tool properties that control how a player holds a tool include: Up, Right, and Pos properties. All of these properties are stored in a single CFrame in the Tool.Grip property.

In order to change a tool's grip properties, you must either use a Script or LocalScript or a plugin such as the Tool Grip Editor example.

Code Samples

Grip Stick

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)

GripUp

hidden
not replicated
read parallel

The GripUp property is one of the properties that specifies a Tool's orientation in a character's hand. This represents the R01, R11, and R21 values of the Grip CFrame's rotation matrix.

Other tool properties that control how a player holds a tool include: Right, Forward, and Pos properties. All of these properties are stored in a single CFrame in the Tool.Grip property.

In order to change a tool's grip properties, you must either use a Script or LocalScript or a plugin such as the Tool Grip Editor example.

Code Samples

Grip Stick

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)

ManualActivationOnly

read parallel

The ManualActivationOnly property controls whether the Tool can be activated without explicitly executing Tool:Activate() in a script.

When set to true, the tool will only fire Tool.Activated when Tool:Activate() is called. This also suppresses the ContextActionService:BindActivate() function.

When set to false, mouse clicks (when the tool is equipped) will also fire Tool.Activated.

Code Samples

Sprint Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = Instance.new("Tool")
tool.Name = "Sprint"
tool.RequiresHandle = false
tool.Parent = player:WaitForChild("Backpack")
function toolActivated()
humanoid.WalkSpeed = 30
tool.ManualActivationOnly = true
task.wait(5)
tool.ManualActivationOnly = false
humanoid.WalkSpeed = 16
end
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(function()
humanoid.WalkSpeed = 16
end)

RequiresHandle

read parallel

This property determines whether a Tool functions without a handle.

A tool has a handle when it contains a child part named Handle. Tools with handles typically require the player equipping them to hold an object to use them, for instance weapons. Tools without handles typically don't require the player equipping them to hold anything to use them, for instance "fly" or "summon" tools.

When set to true, the tool will function only with a handle. When set to false, the tool will function even without a handle.

Code Samples

Does a Tool Require a Handle

local tool = script.Parent
if tool.RequiresHandle then
print("This tool requires a handle!")
else
print("This tool does not require a handle!")
end

ToolTip

read parallel

The ToolTip property controls the message that will be displayed when the player's Mouse hovers over the Tool in their Backpack.

Generally, the value of this property should describe the what the tool is or its use. For instance, for a shovel tool, you may choose to set the ToolTip to:


tool.ToolTip = "Shovel"

or


tool.ToolTip = "Use to dig"

or


tool.ToolTip = "Shovel - Use to dig"

Methods

Activate

void

The Activate function simulates a click on a Tool. The Tool must be equipped for this function to work.

Tools will normally trigger the Tool.Activated event when the player presses the left mouse button, while the tool is equipped.

The below code, when placed in a LocalScript, would create a tool in the LocalPlayer's Backpack. It will simulate the tool being activated and print "Tool activated" when the player equips the tool.


local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = game.Players.LocalPlayer.Backpack
tool.Equipped:Connect(function()
tool:Activate()
end)
function toolActivated()
print("Tool activated")
end
tool.Activated:Connect(toolActivated)

Returns

void

Code Samples

Invisibility Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local tool = Instance.new("Tool")
tool.Name = "Invisibility Tool"
tool.RequiresHandle = false
tool.Parent = player.Backpack
local invisible = false
local function toolActivated()
if invisible then
return
end
invisible = true
for _, bodypart in pairs(character:GetChildren()) do
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 1
end
end
task.wait(3)
tool:Deactivate()
task.wait(1)
invisible = false
end
local function toolDeactivated()
if not invisible then
return
end
for _, bodypart in pairs(character:GetChildren()) do
if bodypart.Name ~= "HumanoidRootPart" then
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 0
end
end
end
end
local function toolEquipped()
tool:Activate()
end
tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Deactivated:Connect(toolDeactivated)
tool.Unequipped:Connect(toolDeactivated)

Deactivate

void

The Deactivate function simulates the deactivation of a Tool. The Tool must be equipped for this function to work.

Tools will normally trigger the Tool.Deactivated event when the player releases the left mouse button, while the tool is equipped.

The below code, when placed in a LocalScript, would create a tool in the LocalPlayer's Backpack. It will simulate the tool being deactivated and print "Tool deactivated" when the player equips the tool.


local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = game.Players.LocalPlayer.Backpack
tool.Equipped:Connect(function()
tool:Deactivate()
end)
function toolDeactivated()
print("Tool deactivated")
end
tool.Deactivated:Connect(toolDeactivated)

Returns

void

Code Samples

Change Tool Color on Key Press

local ContextActionService = game:GetService("ContextActionService")
local tool = script.Parent
-- Gives Handle within Tool a random BrickColor
local function toolDeactivated()
tool.Handle.BrickColor = BrickColor.random()
end
-- Deactivates the tool when any key is pressed down
local function keyPressed(_actionName, actionInputState, _actionInputObject)
if actionInputState == Enum.UserInputState.Begin then
tool:Deactivate()
end
end
tool.Deactivated:Connect(toolDeactivated)
ContextActionService:BindAction("deactivateTool", keyPressed, true, "x")
Invisibility Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local tool = Instance.new("Tool")
tool.Name = "Invisibility Tool"
tool.RequiresHandle = false
tool.Parent = player.Backpack
local invisible = false
local function toolActivated()
if invisible then
return
end
invisible = true
for _, bodypart in pairs(character:GetChildren()) do
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 1
end
end
task.wait(3)
tool:Deactivate()
task.wait(1)
invisible = false
end
local function toolDeactivated()
if not invisible then
return
end
for _, bodypart in pairs(character:GetChildren()) do
if bodypart.Name ~= "HumanoidRootPart" then
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 0
end
end
end
end
local function toolEquipped()
tool:Activate()
end
tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Deactivated:Connect(toolDeactivated)
tool.Unequipped:Connect(toolDeactivated)

Events

Activated

Activated isn't called if the Ctrl key is pressed during a click.

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 LocalPlayer's 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)

Code Samples

Creating a Colorful Brick Tool

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character.Humanoid
-- Create a new randomly colored part at *pos* world position
local function spawnPart(position)
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(1, 1, 1)
part.Position = position
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 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
-- Make a new tool when the LocalScript first runs
makeTool()
Player Fly Tool

local Players = game:GetService("Players")
local POWER = 30
local bpos = Instance.new("BodyPosition")
local gyro = Instance.new("BodyGyro")
local fly = false
local tool = nil
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local char = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local function setupTool()
tool = Instance.new("Tool")
tool.Name = "Fly"
tool.RequiresHandle = false
tool.Parent = player.Backpack
gyro.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
bpos.maxForce = Vector3.new(math.huge, math.huge, math.huge)
tool.Parent = player.Backpack
end
setupTool()
local function onSelected()
bpos.Parent = char
bpos.position = char.Position + Vector3.new(0, 10, 0)
gyro.Parent = char
character.Humanoid.PlatformStand = true
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 0
v.CurrentAngle = -1
if v.Name == "Left Hip" then
v.CurrentAngle = 1
end
end
end
fly = true
task.wait()
while fly do
local pos = mouse.Hit.Position
gyro.CFrame = CFrame.new(char.Position, pos) * CFrame.fromEulerAnglesXYZ(-3.14 / 2, 0, 0)
bpos.Position = char.Position + (pos - char.Position).Unit * POWER
task.wait()
end
end
function onDeselected()
gyro.Parent = nil
fly = false
character.Humanoid.PlatformStand = false
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 1
end
end
bpos.Parent = nil
end
tool.Unequipped:Connect(function()
fly = false
end)
tool.Activated:Connect(onSelected)
tool.Deactivated:Connect(onDeselected)

Deactivated

The Deactivated event fires when the left mouse button is released while a Tool is equipped.

This function is used to perform an action when the player stops using a tool. For instance, a tool may make a player fly until they release their left mouse button.

The below code, when placed in a LocalScript, would create a tool in the LocalPlayer's Backpack. It will print "Tool deactivated" when the player releases the left mouse button, while the tool is equipped.


local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = game.Players.LocalPlayer.Backpack
function toolDeactivated()
print("Tool deactivated")
end
tool.Deactivated:Connect(toolDeactivated)

Code Samples

Player Fly Tool

local Players = game:GetService("Players")
local POWER = 30
local bpos = Instance.new("BodyPosition")
local gyro = Instance.new("BodyGyro")
local fly = false
local tool = nil
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local char = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local function setupTool()
tool = Instance.new("Tool")
tool.Name = "Fly"
tool.RequiresHandle = false
tool.Parent = player.Backpack
gyro.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
bpos.maxForce = Vector3.new(math.huge, math.huge, math.huge)
tool.Parent = player.Backpack
end
setupTool()
local function onSelected()
bpos.Parent = char
bpos.position = char.Position + Vector3.new(0, 10, 0)
gyro.Parent = char
character.Humanoid.PlatformStand = true
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 0
v.CurrentAngle = -1
if v.Name == "Left Hip" then
v.CurrentAngle = 1
end
end
end
fly = true
task.wait()
while fly do
local pos = mouse.Hit.Position
gyro.CFrame = CFrame.new(char.Position, pos) * CFrame.fromEulerAnglesXYZ(-3.14 / 2, 0, 0)
bpos.Position = char.Position + (pos - char.Position).Unit * POWER
task.wait()
end
end
function onDeselected()
gyro.Parent = nil
fly = false
character.Humanoid.PlatformStand = false
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 1
end
end
bpos.Parent = nil
end
tool.Unequipped:Connect(function()
fly = false
end)
tool.Activated:Connect(onSelected)
tool.Deactivated:Connect(onDeselected)

Equipped

The Equipped event fires when a player when a player takes a Tool out of their Backpack to use. This event can be used to determine when a player stops using and puts a tool away.

This event doesn't fire when Tool.RequiresHandle is enabled and no handle is present.

The opposite of this event, Tool.Unequipped, can be used alongside this event to determine unequips a Tool by putting in back in their backpack.

Parameters

mouse: Mouse

The player's mouse.


Code Samples

Print when a Player Equips a Tool

local Tool = script.Parent
local function onEquipped(_mouse)
print("The tool was equipped")
end
Tool.Equipped:Connect(onEquipped)

Unequipped

The Unequipped event fires when a player unequips a Tool by putting in back in their Backpack. This event can be used to determine when a player stops using and puts a tool away.

This event doesn't fire when Tool.RequiresHandle is enabled and no handle is present.

The opposite of this event, Tool.Equipped, can be used alongside this event to determine when a player takes a tool out of their backpack to use.

The example shown below will print "A tool was unequipped" each time the tool is unequipped by the player. Please note that the below example assumes that you've already defined what "Tool" is.


Tool.Unequipped:Connect(function()
print("The tool was unequipped")
end)

Code Samples

Player Fly Tool

local Players = game:GetService("Players")
local POWER = 30
local bpos = Instance.new("BodyPosition")
local gyro = Instance.new("BodyGyro")
local fly = false
local tool = nil
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:Wait()
end
local char = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local function setupTool()
tool = Instance.new("Tool")
tool.Name = "Fly"
tool.RequiresHandle = false
tool.Parent = player.Backpack
gyro.maxTorque = Vector3.new(math.huge, math.huge, math.huge)
bpos.maxForce = Vector3.new(math.huge, math.huge, math.huge)
tool.Parent = player.Backpack
end
setupTool()
local function onSelected()
bpos.Parent = char
bpos.position = char.Position + Vector3.new(0, 10, 0)
gyro.Parent = char
character.Humanoid.PlatformStand = true
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 0
v.CurrentAngle = -1
if v.Name == "Left Hip" then
v.CurrentAngle = 1
end
end
end
fly = true
task.wait()
while fly do
local pos = mouse.Hit.Position
gyro.CFrame = CFrame.new(char.Position, pos) * CFrame.fromEulerAnglesXYZ(-3.14 / 2, 0, 0)
bpos.Position = char.Position + (pos - char.Position).Unit * POWER
task.wait()
end
end
function onDeselected()
gyro.Parent = nil
fly = false
character.Humanoid.PlatformStand = false
for _, v in ipairs(char:GetChildren()) do
if v.ClassName == "Motor" then
v.MaxVelocity = 1
end
end
bpos.Parent = nil
end
tool.Unequipped:Connect(function()
fly = false
end)
tool.Activated:Connect(onSelected)
tool.Deactivated:Connect(onDeselected)