Why is this debounce not working

This debounce just doesn’t seem to work. If you click too quickly, the debounce just fails.
Why?

local damage = {}
local config
local debounce = false
function damage.IdentifyGun(gunname, target)
config = script.Configuration
local folder = config:FindFirstChild(gunname)
local damagevar = folder.Damage
local ammovar = folder.Ammo
local fireratevar = folder.FireRate
local reloadtimevar = folder.ReloadTime
local maxammovar = folder.MaxAmmo
if target == nil then
	return
end
if target.Parent:FindFirstChild("Humanoid") then
	if debounce == false then
	if ammovar.Value > 0  then
		debounce = true
		ammovar.Value -=1
		target.Parent.Humanoid:TakeDamage(damagevar.Value)
task.wait(fireratevar.Value)
		if ammovar.Value == 0 then
debounce = true
task.wait(reloadtimevar)
debounce = false
			return
		end
	end  
end
	end
end
function damage.Effect(muzzleeffect)
	muzzleeffect.Enabled = true
	task.wait(.25)
	muzzleeffect.Enabled = false
end
return damage
5 Likes

Does it give any output? Error, warning, message?

3 Likes

Not at all, I tried debugging it but to no avail
The deboune value seems to not be being set to true

1 Like

Before return at “if ammovar.Value == 0” try to print debounce. Does it give false, or doesnt give anything?
If it doesnt give anything, it seems like a problem with task.wait, experienced by myself in past.

1 Like

Did a bit of changes and tried what you did, it seems like there’s ANOTHER problem

local damage = {}
local config
local debounce = false
function damage.IdentifyGun(gunname, target)
config = script.Configuration
local folder = config:FindFirstChild(gunname)
local damagevar = folder.Damage
local ammovar = folder.Ammo
local fireratevar = folder.FireRate
local reloadtimevar = folder.ReloadTime
local maxammovar = folder.MaxAmmo
if target == nil then
	return
end
if target.Parent:FindFirstChild("Humanoid") then
	if debounce == false then
	if ammovar.Value > 0  then
		print("aaq")
		debounce = true
		ammovar.Value -=1
		print(ammovar.Value)
		target.Parent.Humanoid:TakeDamage(damagevar.Value)
task.wait(fireratevar.Value)
		if ammovar.Value == 0 then
debounce = true
task.wait(reloadtimevar.Value)
print("mhmn")
print(debounce)
debounce = false
			return
		end
	end  
end
	end
end
function damage.Effect(muzzleeffect)
	muzzleeffect.Enabled = true
	task.wait(.25)
	muzzleeffect.Enabled = false
end
return damage

It seems to be that ammovar.Value > 0 only runs once?
I’m not sure why and ammovar is not 0, it’s 31 as it only runs once like I said

1 Like

Fixed that problem of the script, debounce still does not work however.

Try printing reloadtimevar to see what your task.wait time is being set to. If the script isn’t getting the right number then it might be setting it to 0.
reloadtimevar has to be a decent time, not something low like .1

I checked reload time, it’s set at 2
There should be no problem with that

But that’s what I want you to check with the print statement, to see if it’s getting the right value. If the value is incorrect (like 0) then your debounce is useless.

Instead of task.wait(fireratevar.Value) you could easily change it to task.wait(5) so when you test you can see if the debounce works properly.

I think I should have clarified, I also used a print statement to check.

1 Like

Looks like I tricked MYSELF with the muzzle effect and it looked like the debounce wasn’t working, when it reality it was

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.