PcoWSkbVqDnWTu_dm2ix
Fading Trap
Part 2 - Debouncing
Fading Trap
Part 2 - Debouncing

In the Deadly Lava course, you learned that the Touched event runs each time a player’s body part comes into contact with the part. This behavior causes issues when a player runs across the fading platform: the function will run multiple times, resetting the loop each time.

For the code to work properly, the function should only run once when the player touches the platform for the first time. Ensuring that an action is only triggered once when it would otherwise be triggered multiple times is known as debouncing.

Debounce Variable

To debounce a function, a boolean variable can be used to keep track of when the platform has already been touched. Boolean just means the only values it can contain are true and false.

  • Create a variable called isTouched and set it to false.
local platform = script.Parent

local isTouched = false

local function fade()
	for count = 1, 10 do
		platform.Transparency = count / 10
		wait(0.1)
	end
platform.CanCollide = false
	wait(3)
	platform.CanCollide = true
	platform.Transparency = 0
end

platform.Touched:Connect(fade)

Checking the Variable

An if statement can be used to only run the code in the fade function if the isTouched debouncing variable is false.

  • Wrap the body of the fade function in an if statement with the condition not isTouched.
local platform = script.Parent

local isTouched = false

local function fade()
	if not isTouched then
		for count = 1, 10 do
            platform.Transparency = count / 10
            wait(0.1)
	    end
		platform.CanCollide = false
		wait(3)
		platform.CanCollide = true
		platform.Transparency = 0
	end
end

platform.Touched:Connect(fade)
Negate Operator

The Lua not operator reverses the value of whatever follows it. In conditional terms, this means that the if statement on the left behaves the same as either statement on the right.

if not isTouched then

end




if isTouched == false then

end

if isTouched == nil then

end

Toggling the Debounce

Currently, the code in the fade function will always run because isTouched is false and not isTouched evaluates to true. To complete the debounce routine, you’ll need to toggle the variable’s value in two places.

  1. Set isTouched to true inside the if statement, before the platform begins to fade.

  2. Set it back to false once the platform has reappeared.

    local function fade()
    	if not isTouched then
           isTouched = true
    		for count = 1, 10 do
    		    platform.Transparency = count / 10
    		    wait(0.1)
    	    end
    		platform.CanCollide = false
    		wait(3)
    		platform.CanCollide = true
    		platform.Transparency = 0
           isTouched = false
    	end
    end
    
    platform.Touched:Connect(fade)

And that’s it! Test your code now, and you should find that the platform fades away when the player jumps on it and comes back a few seconds later.

You can duplicate this platform across a wider gap to create a challenging obstacle and change the speed at which they fade to balance the difficulty.

local platform = script.Parent

local isTouched = false

local function fade()
	if not isTouched then
		isTouched = true
		for count = 1, 10 do
		    platform.Transparency = count / 10
		    wait(0.1)
	    end
		platform.CanCollide = false
		wait(3)
		platform.CanCollide = true
		platform.Transparency = 0
		isTouched = false
	end
end

platform.Touched:Connect(fade)


Previous Page Getting Started