Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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)
0
You basically put it in a loop, because RenderStepped fires 60 times every second, and your print should run once every 5 seconds. protectiveebob 221 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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

0
thx! PaliKai13 92 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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
0
You should use the event instead, there is a reason the event exists BlackOrange3343 2676 — 5y
0
Oh, I wasn't aware of this event. I learned something myself then! SteelMettle1 394 — 5y

Answer this question