Is there a way to make a touched event wait to fire?

Example:

Local Part = script.Parent

Part.Touched:Connect(function(Hit)
--The code...
wait(1)
end)

This is corret? if no, how i make a touched event wait to fire???

1 Like

You don’t. You add a debounce:

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(Hit)
    if Debounce = false then
        Debounce = true
        --The code...
        wait(1)
        Debounce = false
    end
end)

I think you’re looking for something like

Local Part = script.Parent
local deb = false

Part.Touched:Connect(function(Hit)
	if deb then return end 
	deb = true
--The code...
	wait(1)
	deb = false
end)

With the debounce, it will run the stuff in there, but before that it will set a variable to true, which preents any future touched calls from working until at the end where it’s set to false

Basically what @BenMactavsin said faster than me

No no, this is not what i want, if you do this:

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(Hit)
    print("Fire")
    if Debounce == false then
        Debounce = true
        --The code...
        wait(1)
        Debounce = false
    end
end)

You will see that it prints several times on the console

Yes, that’s because a Touched event will fire when something touches the part, so code that isn’t debounced will be printed anyways, even if other code is waiting because of a debounce. You have to put code in a debounce if you want it to wait before executing the code again, for now hopefully

Something related but also unrelated, Roblox in the future may just have a property to do this called CanTouch, which isn’t out yet although I may be wrong, and I’m not sure how it would work exactly if it was/is out

Shouldn’t you put the wait() at the start of the Touched event if you want it to delay/wait a bit until it fires?

local Part = script.Parent
local DB = false

Part.Touched:Connnect(function(Hit)
    wait(5)
    if DB == false then
        DB = true
        print("I am a delayed touched event")
        --Now do your code here
        wait(1)
        DB = false
    end
end)

Or you could use Part.Touched.Wait()? Not sure