Need help w a touched?

ive been looking for debounces and they’ve all had the same result

it keeps playing over n over again instead of once

if effect == "BlackFlash" then
		if not UniversalDebounce then
			player.Character["Left Arm"].Touched:Connect(function(hit)
				UniversalDebounce = true
				    if hit.Parent:FindFirstChild("Humanoid") then
					
					if hit.Parent.Name == player.Character.Name then
						return
					else
						Module.BlackFlash(player.Character,hit.Parent)
						wait(5)
						UniversalDebounce = false
					end
				end	
			end)
		end	
	end

This should work:

local debounceCooldown = 5  -- Set the debounce cooldown time in seconds
local lastTriggerTime = 0   -- Initialize the last trigger time

if effect == "BlackFlash" then
    player.Character["Left Arm"].Touched:Connect(function(hit)
        local currentTime = tick()  -- Get the current time
        
        if currentTime - lastTriggerTime >= debounceCooldown then
            lastTriggerTime = currentTime
            
            if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
                if hit.Parent.Name == player.Name then
                    return
                else
                    Module.BlackFlash(player.Character, hit.Parent)
                end
            end
        end
    end)
end

It worked, Thank you, Also if you don’t mind could you explain what was wrong?

In your initial code, the primary issue was that the UniversalDebounce variable was being set to true when the event handler was triggered, but it was reset to false after a wait(5) inside the event handler. This meant that as soon as the wait(5) finished, the UniversalDebounce was reset and the event handler could trigger again immediately, resulting in the event triggering repeatedly as long as there were touch events.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.