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)
Hello! My name is BlackOrange3343 and I will be helping you today!
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)
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
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