Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't debounce work? Should print once, but instead prints many more times.

It prints more than one time. I only want it to print once.

RunService.RenderStepped:Connect(function()
local debounce = false
	if not debounce and not character:FindFirstChild("UpperTorso") then
		debounce = true
		beam.Enabled = false
		print(head.Parent.Name .. " has died")
		wait(5)
		debounce = false
	end
end)

1 comment

0

You basically put it in a loop, because RenderStepped fires 60 times every second, and your print should run once every 5 seconds.

2 answers

✓ Accepted answer

Hello! My name is BlackOrange3343 and I will be helping you today!

#Problem:# So the reason the debounce isn't actually working is that each time the event RenderStepped runs, it creates a new local variable called debounce. This means it will indeed spam.

One more issue is you did not define RunService. (Maybe you have but didn't show it)

#Solution:# The solution is very simple. All you have to do is move local debounce = false outside of the event!

local RunService = game:GetService('RunService')

local debounce = false

RunService.RenderStepped:Connect(function()
    if not debounce and not character:FindFirstChild("UpperTorso") then
        debounce = true
        beam.Enabled = false
        print(head.Parent.Name .. " has died")
        wait(5)
        debounce = false
    end
end)

note that some of the variables inside the event are not defined. Make sure to define them and everything should work!

Best of luck developer and I hope this helped!

BlackOrange3343

1 comment

0

thx!

Your code is actually structured like this:

local debounce = false
while true do
    if not debounce then
        if not character:FindFirstChild("UpperTorso") then
            debounce = true
            beam.Enabled = false
            print(head.Parent.Name .. " has died")
            wait(5)
            debounce = false
        end
    end
    wait()
end

So, the code declares a debounce to false, then constantly run a while loop that will never stop. The code will constantly check if debounce isn't false (so, it's an if it is true).

To fix this, you just need to make a few modifications to the script:

local debounce = true -- I personally don't like double negatives
while debounce do	--debounce is initially set to true so it will run
	debounce = false  --debounce is set to false so it immediately starts working
	if not character:FindFirstChild("UpperTorso") then
		beam.Enabled = false
		print(head.Parent.Name .. " has died")
		wait(5)
   	end
	wait()
	debounce = true -- Allows it to run the next time
end

2 comments

0

You should use the event instead, there is a reason the event exists

0

Oh, I wasn't aware of this event. I learned something myself then!

This discussion is preserved as it was. New answers, comments, and votes are closed.