PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Activated

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 Players/LocalPlayer|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

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()

Player Fly Tool

The code below demonstrates the functionality of several Tool events, including Tool/Activated, Tool/Deactivated, and Tool/Unequipped.

The code, when placed in a LocalScript in the StarterPlayer.StarterCharacterScripts folder inserts and controls a tool in the Players/LocalPlayer|LocalPlayer's 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.

-- ========================================
-- GLOBAL VARIABLES
-- ========================================
local power = 30
wait() 
local tool = nil

local bpos = Instance.new("BodyPosition") 
local gyro = Instance.new("BodyGyro") 

local fly = false 

local player = game.Players.LocalPlayer 

local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local char = character:FindFirstChild("UpperTorso")
if not char then char = character:FindFirstChild("Torso") end

local mouse = game.Players.LocalPlayer:GetMouse()
-- ========================================
-- ========================================


-- ========================================
-- SETUP FUNCTION
-- ========================================

-- Create a new tool and place it in the local player's backpack
-- Make this script a child of the tool, script controls flying
local function setupTool()
	tool = Instance.new("Tool")
	tool.Name = "Fly"
	tool.RequiresHandle = false
	tool.Parent = game.Players.LocalPlayer.Backpack
	
	gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge) 
	bpos.maxForce = Vector3.new(math.huge,math.huge,math.huge)
	
	script.Parent = tool
	
	tool.Parent = game.Players.LocalPlayer.Backpack
end

setupTool()
-- ========================================
-- ========================================


-- ========================================
-- FLY FUNCTIONS
-- ========================================

-- Start flying when player uses tool
function onSelected() 	
	bpos.Parent = char 
	bpos.position = char.Position + Vector3.new(0,10,0) 
	gyro.Parent = char 
	
	character.Humanoid.PlatformStand = true 

	for i, 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 
	wait()
	while fly do
		local pos = mouse.Hit.p
		gyro.CFrame = CFrame.new(char.Position,pos) * CFrame.fromEulerAnglesXYZ(-3.14/2,0,0) 
		bpos.Position = char.Position + (pos-char.Position).unit * power 
		wait()
	end 
end 

-- Stop flying when player stops using tool
function onDeselected() 
	gyro.Parent = nil 
	fly = false 
 
	character.Humanoid.PlatformStand = false 
	
	for i, 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)
-- ========================================
-- ========================================