How would I fix debounce on this script

Hello devforum.

I wanted to make a flashlight tool and I made it work perfectly so far but the player is able to spam it and it’s breaking the script. So since I heard about debounce before, I decided to check it out and tried to insert it inside my script, but it didn’t work.

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


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)



flashlight.Equipped:Connect(function() 
	isEquipped = true
	if isEquipped then
	OpenAnimTrack:Play()
	OpenAnimTrack.Stopped:Connect(function()
		HoldAnimTrack:Play()
	end)
	OpenAnimTrack:GetMarkerReachedSignal("Open"):Connect(function()
		flashlight.Light.SpotLight.Enabled = true
		print("On")
		end)
	end
end)

flashlight.Unequipped:Connect(function()
	isEquipped = false
	if isEquipped then
	HoldAnimTrack:Stop()
		flashlight.Light.SpotLight.Enabled = false
		print("Off")
		end
			end)

I would appreciate any help. Thanks!

You need to change the value of the debounce after the “if” statement

IsEquipped = true
if IsEquipped then -- the condition is always true
   --This will always activate
end

This is how it should be formatted:

local isEquipped = true

Instance.Event:Connect(function() 
   if isEquipped then

       isEquipped = false
       -- do something, yield
      task.wait(0.5)
      isEquipped = true

   end
end)
3 Likes

Well I tried this and it didn’t work. Did I do something wrong?

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


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)



flashlight.Equipped:Connect(function() 
	if isEquipped then

		isEquipped = false
	isEquipped = true
	if isEquipped then
	OpenAnimTrack:Play()
	OpenAnimTrack.Stopped:Connect(function()
		HoldAnimTrack:Play()
	end)
	OpenAnimTrack:GetMarkerReachedSignal("Open"):Connect(function()
		flashlight.Light.SpotLight.Enabled = true
				print("On")
				task.wait(0.5)
				isEquipped = true
			end)
			end
	end
end)

flashlight.Unequipped:Connect(function()
	HoldAnimTrack:Stop()
		flashlight.Light.SpotLight.Enabled = false
		print("Off")

	end)