Simple debounce fails to work on debris

Hello! I’m making a silly little banana tool, however the debounce is failing to work and is making the whole ordeal annoying. I have tried to fix it, my friend has tried to fix it. Heck I even used chatgpt, so if you have even the slightest idea, I would be delighted.

Here is my script:

wait(1)

local banana = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local ragdollRemote = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("Ragdoll")
local debounce = false

function onTouched(part)
	local character = part:FindFirstAncestorWhichIsA("Model")
	local humanoid = character and character:FindFirstChild("Humanoid")

	if humanoid and not debounce then
			debounce = true
			banana.Scream:Play()
			banana.SlipSound:Play()
			task.delay(3.5, function()
				debounce = false
			end)

		ragdollRemote:Fire(character, 3)
	end
end

banana.Touched:Connect(onTouched)

I fixed your code a little and now should work:

local banana = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local ragdollRemote = replicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("Ragdoll")
local debounce:boolean = false

function onTouched(part:BasePart):()
if debounce==true then return end
	local character:Instance = part.Parent
if not character:IsA("Model") then return end
	local humanoid:Humanoid? = character:FindFirstChild("Humanoid")

	if humanoid==nil then return end
			debounce = true
			banana.Scream:Play()
			banana.SlipSound:Play()
ragdollRemote:Fire(character, 3)
			task.wait(3.5)
debounce = false

		
	
end

banana.Touched:Connect(onTouched)
1 Like

Debounce looks like it should work as intended to me. (for one player at a time)
Not sure about the rest of the script…

1 Like

Yarik thank you for your help again! However now instead of playing at random intervals. The script always plays twice with about a 10 millisecond interval. This is significant progress but I am still so confused.

i updated the code a little;
It should work now ig

Still doubles on the function. Another thing I’ve noted is that everytime the debris is removed the game freezes for about a second. Maybe I’m spawning this twice somehow? If so here’s the script for that.

local tool = script.Parent
local handle = tool.Handle
local placesound = tool.PlaceSound
local spawncooldown = 10
local debounce = true

local function spawn()
	debounce = false
	local part = handle:Clone()
	placesound:Play()
	part.Parent = game.Workspace
	part.Position = handle.Position
	part.CanCollide = true
	part.Orientation = Vector3.new(0,0,0) 
	game.Debris:AddItem(part,10)
end

tool.Activated:Connect(function()
	if debounce == true then
		spawn()
		wait(spawncooldown)
		debounce = true
	end
end)

Haha I thought the same thing for so long

AS in I don’t think the problem is the debounce

May not be your problem here but, I like to always set the flag right way.
just to be sure nothing else get’s by while doing other things.
if debounce then debounce = flase

This script seems fine to me, can you add some print statements to check?

Check my message beforehand with the image, it has printed statements in it

Can you show all the code related to it please?

try printing part after:

local humanoid:Humanoid? = character:FindFirstChild("Humanoid")

Needs more debugging ig

This is where my connected code is

I don’t see any issues with this code, can you please show everything related to the tool?

As I’ve just tested setting the flag before hand doesn’t fix the issue. Return ends get in the way and like I did before, it just gave me a chance for the script to play out but still multiple times

Thats why im so confused D:, that’s it. That’s all the code

Debounce is always true


tool.Activated:Connect(function()
	if debounce == true then
debounce = false
		spawn()
		task.wait(spawncooldown)
		debounce = true
	end
end)


debounce turns off at the beginning of the function. And for the printing here is the output after one use.

image

That way too hacky even for me :skull:
That makes sense why it fires, now try to put print under:

if humanoid==nil then return end
1 Like

This is what I was talking about…

1 Like