Help with debounce inside tool

Hello devforum!

I made this flashlight tool that when you equip it, it plays some animations and worked perfectly fine until I inserted some debounce thingy.
The animations don’t play,(they only play if I spam it) and the debounce doesn’t work either and I get no error
I don’t know whats the issue exactly so I couldn’t search anything in the devforum.

local Players = game:GetService("Players")
local flashlight = script.Parent

local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local openAnim = Instance.new("Animation")
openAnim.AnimationId = "rbxassetid://11750752863"
local OpenAnimTrack = animator:LoadAnimation(openAnim)

local holdAnim = Instance.new("Animation")
holdAnim.AnimationId = "rbxassetid://11684755470"
local HoldAnimTrack = animator:LoadAnimation(holdAnim)

local debounce = false
local cd = 3

flashlight.Equipped:Connect(function()
	if debounce == false then
		debounce = true
		wait(cd)
		debounce = false
		OpenAnimTrack:Play()
		OpenAnimTrack.Stopped:Connect(function()
			HoldAnimTrack:Play()
		end)
	end
end)
flashlight.Unequipped:Connect(function()
	if debounce == true then
		humanoid:UnequipTools()
		HoldAnimTrack:Stop()
	end
end)

Thanks for your attention.

local Players = game:GetService("Players")
local flashlight = script.Parent

local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local openAnim = Instance.new("Animation")
openAnim.AnimationId = "rbxassetid://11750752863"
local OpenAnimTrack = animator:LoadAnimation(openAnim)

local holdAnim = Instance.new("Animation")
holdAnim.AnimationId = "rbxassetid://11684755470"
local HoldAnimTrack = animator:LoadAnimation(holdAnim)

local debounce = false
local cd = 3

flashlight.Equipped:Connect(function()
	if (debounce == true) then 
		return
	end 
	debounce = true
	OpenAnimTrack:Play()
	OpenAnimTrack.Stopped:Connect(function()
		HoldAnimTrack:Play()
	end)
	task.wait(cd)
	debounce = false
end)

flashlight.Unequipped:Connect(function() --// The debounce here would stop the hold animation from stopping
	humanoid:UnequipTools()
	HoldAnimTrack:Stop()
end)
1 Like

You can avoid needing to yield by using a function like tick() to store the time at which the function was last called, and if the time difference is below the treshold, do nothing. An example is below.

local last, cooldown = nil, 3 -- (seconds)

local function doMyThing(): ()
    if last and tick() - last < cooldown then
        return
    end
    last = tick()

    -- Do your thing.
end
1 Like

The debounce doesn’t work, I’m able to spam the flashlight

robloxapp-20221216-0913448.wmv (497.6 KB)
Spamming it like mad … what seems to be the problem.

1 Like

robloxapp-20221216-1834151.wmv (489.7 KB)
I want to make it so when you equip it, you cant unequip it until you completed all the animations and to equip it again, you have to wait the cooldown but instead once you equip it you can unequip it while the animation is playing and when you are waiting for the cooldown it just doesn’t play the animations.

Hey there, use .CanBeDropped to make sure they can’t drop the tool, and whenever the player unequips the tool make it auto equip it (for this you also need to make it play the animation only if the animation isn’t already playing using .IsPlaying).