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

How to stop tool animation when tool is unequipped?

I inserted local script in the tool and now it plays animation when I press "R", but it still plays animation after tool is unequipped, how to fix that? my current script:

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

local AnimationID = 'rbxassetid://4868889544'

local key = 'R'
local debounce = true

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if  IsTyping then return end
	if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
		debounce = false
		local Animation = Instance.new("Animation")
		Animation.AnimationId = AnimationID
		local LoadAnimation = Humanoid:LoadAnimation(Animation)
		LoadAnimation:play()
		wait(5)
		debounce = true
		
		end
	end)

2 answers

✓ Accepted answer

You could do something like this:

local enabled = false

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	If IsTyping or not enabled then return end
	-- code
end)

tool.Equipped:Connect(function()
	enabled = true
end)

tool.Unequipped:Connect(function()
	enabled = false
end)

Basically this lets you know if the player has the tool equipped.

1 comment

0

I didn't add all of the declared variables, but you should get the gist.

You could try using the Tool.Unequipped event! This fires every time a tool is unequipped. You can read about Tool.Unequipped here!

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