Debounce not working

Hello, I have a code so written so that if you kill a ninja (npc) the counter will go down, but for some reason the debounce doesn’t work resulting that when you kill a ninja the counter can jump from 3 down to - 16 but it varies

This is the code for the enemy ninja (npc)

This is the code that collects the quest information etc

1 Like

Could you copy and paste it here? Thanks amigo.

image

task.wait(5) --change 5 to cooldown length
debounce = false

Add that above the end statement after the FireClient() call.

1 Like

Here you go!

npc code:

coroutine.resume(coroutine.create(function()
local anim = Instance.new(“Animation”)
anim.AnimationId = “rbxassetid://8016773042” --your assetID here
local track = script.Parent.Humanoid.Animator:LoadAnimation(anim)
track:Play()
end))

local Debounce = false

script.Parent.Humanoid.Died:Connect(function()
script.Parent.Torso.Touched:Connect(function(hit)
if hit:FindFirstChild(“PlayerName”) then
if Debounce == false then
Debounce = true
local player = hit.PlayerName.Value
game.ReplicatedStorage.Events.IsTemplateEnabled:FireClient(player)
end
end
end)
end)

game.ReplicatedStorage.Events.TemplateIsEnabled.OnServerEvent:Connect(function(player)
game.ReplicatedStorage.Events.Quests:FireClient(player, “KilledNinja”)
end)

code2:

game.ReplicatedStorage.Events.Quests.OnClientEvent:Connect(function(data)
if data == “KilledNinja” then
NumberToKillLeft = NumberToKillLeft - 1
script.Parent.Background.Info.Text = “Kill “…NumberToKillLeft…” more ninja”

	if NumberToKillLeft == 0 then
		coroutine.resume(coroutine.create(function()
			write(script.Parent.Background.Info, "Mission complete, return to the Hokage")
		end))
		script.Parent.Background.ImageLabel.Image = "http://www.roblox.com/asset/?id=7882925101"
	end
end

end)

Not sure if you saw but I replied with the fix above.

unfortunately that doesn’t work

if Debounce == false then
	Debounce = true
	local player = hit.PlayerName.Value
	game.ReplicatedStorage.Events.IsTemplateEnabled:FireClient(player)
	task.wait(5)
	Debounce = false
end

ye ik, I tried that and it doesn’t work because whats the point in making the debounce false if the npc is dead

Well you set the debounce to true and never back to false again, so you need to find the place most suitable to set the debounce back to false.

1 Like

I see u nvr changed the debounce val after changing it to false, mite tht be the problem?

1 Like

hmmm, maybe but I dont think so because they die anyway

local Debounce = false
--NumberToKillLeft this isnt defined
--write this function isnt defined
--also add a debounce here

game.ReplicatedStorage.Events.Quests.OnClientEvent:Connect(function(data)
	if data == "KilledNinja" then
		if Debounce then
			return
		end
		Debounce = true
		NumberToKillLeft = NumberToKillLeft - 1
		script.Parent.Background.Info.Text = "Kill "..NumberToKillLeft.." more ninja"
		task.wait(10)
		Debounce = false
		if NumberToKillLeft == 0 then
			coroutine.resume(coroutine.create(function()
				write(script.Parent.Background.Info, "Mission complete, return to the Hokage")
			end))
			script.Parent.Background.ImageLabel.Image = "http://www.roblox.com/asset/?id=7882925101"
		end
	end
end)
1 Like

The debounce is defined I just don’t show some of the script, and anyway they have to kill the ninja 3 times

I’ve added a debounce to the local script, try it now & I was just making sure those things were being correctly defined on your end keep the debounce change in the server script too.

yes but that debounce accounts for whenever the player kills a ninja, so if they kill a ninja in the space of 10 seconds then it wont count

Yes, it can always be shortened. Just change 10 inside the brackets. That’s the whole purpose of a debounce, to prevent a function/block of code from executing too frequently.

NumberToKillLeft = #workspace:FindFirstChild("NinjaFolder"):GetChildren()

You could also get the number of ninjas left by doing something like this.

But that is going off in a completely different direction and that is not my problem. My problem is the debounce doesn’t work and so the event fires multiple times, I worked out it has nothing to do with that script

Capture