How do I add a wait timer to this?

I’m trying to add a wait timer to my spleif game script so that you don’t fall instantly when you touch a brick.
Here’s the script:

local spleif = script.Parent

function onTouch(hit)
	if not hit.Parent:findFirstChild("Humanoid") then return end
	spleif.Transparency = (.65)
	spleif.CanCollide = false

	wait(7.5)

	spleif.Transparency = (0)
	spleif.CanCollide = true
end
spleif.Touched:Connect(onTouch)

How do I add a wait timer to this?

I don’t understand. Dont you already have a wait(7.5) on line 8?

That is for making the brick re-appear after you step on it. I’m trying to make something like this:

local spleif = script.Parent

function onTouch(hit)
    wait(1) <-------
	if not hit.Parent:findFirstChild("Humanoid") then return end
	spleif.Transparency = (.65)
	spleif.CanCollide = false

	wait(7.5)

	spleif.Transparency = (0)
	spleif.CanCollide = true
end
spleif.Touched:Connect(onTouch)

You just add it. Also, adding a debounce is also important to make sure the part doesn’t appear and dissapear repetitively, because when an event is fired, a new thread is created to handle that callback function which handled the fired event.

How do I do that? I’m fairly new to scripting.

Just add a debounce at the beginning of the function and then add a wait() before setting the debounce back to its original value.

I don’t know how to make a debounce

Set a variable outside of the function set to false.
When the block is touched, check if the debounce var is false. If yes, carry on.
At the end of the function set the debounce to true again.

Ex:

local debounce = false
local spleif = script.Parent
function onTouch(hit)
	if not debounce then
		doThingsHere()
		wait(5)
		reset()
		debounce = false
	end
end
spleif.Touched:Connect(onTouch)
local spleif = script.Parent
local debounce = false

function onTouch(hit)
    if not debounce then
        debounce = true
        wait(1) <-------
	    if not hit.Parent:findFirstChild("Humanoid")     then return end
	    spleif.Transparency = (.65)
	    spleif.CanCollide = false

	    wait(7.5)

	    spleif.Transparency = (0)
	    spleif.CanCollide = true
        debounce = true
        
end
spleif.Touched:Connect(onTouch)

you forgot a second end, wanted to let you know

I know, I purposely do that just to see if they are going to be lazy and copy the code or not, and you just gave it away.

1 Like

It worked after I added

debounce = false

Thanks!

1 Like

Smart. I set both of the value to true, should be false.

I noticed one flaw in this code

if a part that isn’t from the character touches this part, then the debounce will break

Also, he never turned the debounce off lol

well CbrahDev fixed that part of it, but your right