Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this script only work when a certain tool is equipped?

Its a script that plays an animation whenever you click however I have no idea how to make it work on tool equip.

local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
        local Animation = Instance.new("Animation") 
        Animation.AnimationId = "rbxassetid://1630162920" 
        local Track = Humanoid:LoadAnimation(Animation)
        Track:Play()
    end
end)

2 comments

0

tools have an event called 'Equipped'

1 answer

✓ Accepted answer
local UserInputService = game:GetService("UserInputService")
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Equip = false

UserInputService.InputBegan:Connect(function(InputObject)
    if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
	if Equip then
        	local Animation = Instance.new("Animation") 
       		Animation.AnimationId = "rbxassetid://1630162920" 
        	local Track = Humanoid:LoadAnimation(Animation)
        	Track:Play()
    		end
	end
end)

Tool.Equipped:Connect(function()
	Equip = true
end)

Tool.Unequipped:Connect(function()
	Equip = false
end)

I made a debounce called Equip so when you equip it it will be true and when it is unequipped it will be false

This discussion is preserved as it was. New answers, comments, and votes are closed.