Why doesn't my debounce work?

Not sure why my debounce doesn’t work. I’ve looked around but couldn’t find anything.

Code
local StartTutorialEvent = game.ReplicatedStorage.RemoteEvents.StartTutorial
local Part = script.Parent
local Debounce = false

game.Players.PlayerAdded:Connect(function(Player)
    Part.Touched:Connect(function(Contact)
    local Humanoid = Contact.Parent:FindFirstChild("Humanoid")
        if Humanoid then
        if Debounce == false then
        Debounce = true
        StartTutorialEvent:FireClient(Player)
                print("E")
                Debounce = false
            end
        end
    end)    
end)

You’re unnecessarily creating more connections as more player joins, which can cause an issue early on the long run. You should only use Touched event and get the player from the argument returned from it.

local Players = game:GetService("Players")

local StartTutorialEvent = game.ReplicatedStorage.RemoteEvents.StartTutorial
local Part = script.Parent
local Debounce = false


Part.Touched:Connect(function(hit)
	if Debounce then
		return
	end
	Debounce = true
	
	local character = hit:FindFirstAncestorOfClass("Model")
	if not character then
		return
	end
	
	local player = Players:GetPlayerFromCharacter(character)
	if player then
        StartTutorialEvent:FireClient(player)
        print("E")
    end
	-- you might opt for a cooldown here or things might trigger too fast
	Debounce = false
end)

Tried this out and the issue still persists. When the Event is fired, I have it spawn in a Model / Re-Parent the Clone of a model, and it seems to clone it more than once, which is not what I had in mind.

Did I remind you of the comment in the script “you might opt for a cooldown here or things might trigger too fast”? You should either add wait(), which is not totally recommended, or alternatively use a different method that works like a timer.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local StartTutorialEvent = game.ReplicatedStorage.RemoteEvents.StartTutorial
local Part = script.Parent

local debounce = false
local lastTouched

Part.Touched:Connect(function(hit)
	if debounce then
		return
	end
	debounce = true
	lastTouched = os.clock()
	
	local character = hit:FindFirstAncestorOfClass("Model")
	if not character then
		return
	end
	
	local player = Players:GetPlayerFromCharacter(character)
	if player then
        StartTutorialEvent:FireClient(player)
        print("E")
    end
	while os.clock() - lastTouched < 5 do
		RunService.Heartbeat:Wait()
	end
	debounce = false
end)

Add a wait(0.25). The Touched event fires many times within a short amount of time.

I did opt for the wait, but you’ve now said it’s not recommended. May I ask why it’s better to avoid in times like these?

Because wait() is unreliable and can possibly ends up in much longer yield than it is suppose to.
I recommend you to look on CustomWait here on devforum :).

1 Like