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

Deactivate

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

Return Type Summary

No return


Code Samples


Change Tool Color on Key Press

The below code is intended to be inserted into a script within a Tool. The script will deactivate the tool when the user presses the x key, while the tool is equipped. The script also changes the handle’s color when the tool is deactivated.

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