UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

Keep the Bridge from Restarting

To keep the timer from constantly restarting, you’ll need to add another variable that will control whether or not startTimer() is allowed to be called again. This variable will use a new type of value called a boolean. Booleans are written the same way as other variables, but instead of using numbers or strings, they can only be set to true or false.

In this situation, the script will use a boolean to check if the timer is currently running before starting it.

  1. At the top of your script under your variables, create a variable named timerActive and set it to false since nobody has pressed the button yet.
local timerDuration = 5
local timerActive = false
 
local function startTimer()
  1. To makes sure startTimer() only starts when timerActive is false, add a second condition to the if statement in buttonPressed().
local function buttonPressed(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	if humanoid and timerActive == false then
		startTimer()
	end
 
end
  1. To keep startTimer() from restarting before the timer runs out, set the boolean timerActive to true.
local function startTimer()
	timerActive = true
	bridge.Transparency = 0
	bridge.CanCollide = true
  1. Playtest the game and make sure the timer doesn’t restart anymore. While the timer doesn’t restart, it also can’t be used more than once. To make the button reusable, you’ll reset the boolean in the next section.
Troubleshooting Tips

Issue: Timer changes numbers without counting down:

  • Check that you’ve added a second condition in buttonPressed's if statement.
  • Make sure at the beginning of startTimer() that you set timerActive to true.

Reset the Timer

To finish the script, you’ll change the boolean and bridge properties back to what they were at the start of the game so the bridge can be used more than once.

  1. In the startTimer() function, after the for loop, set the bridge back to its original properties by changing the bridge’s transparency to 0.8 and CanCollide to false.
local function startTimer()
    print("Countdown started")
	timerActive = true
	bridge.Transparency = 0
	bridge.CanCollide = true

	-- For loop that counts down from timerDuration
	for count = timerDuration, 0, -1 do
		timerText.Text = count
		wait(1)
	end

	-- Make the bridge not walkable
	bridge.Transparency = 0.8
	bridge.CanCollide = false

end
  1. Right now the display text stays at 0. To make it blank again, change the timerText to an empty string.

bridge.Transparency = 0.8

bridge.CanCollide = false

timerText.Text = ""

  1. Since you’re now done with the timer, set the timerActive boolean to false. This will also allow the timer to restart next time the button is touched.
bridge.Transparency = 0.8
bridge.CanCollide = false
timerText.Text = ""
timerActive = false
  1. Playtest and check to make sure the bridge can be used multiple times.

Finished Project Sample

Project File

Download the finished project here.

Finished Script

local bridge = script.Parent
-- Gets the button as it's typed in the Explorer
local button = game.Workspace.ButtonBridge

-- Gets the part for the display
local timerPart = game.Workspace.TimerDisplay
-- Gets the Text that will display the timer
local timerText = timerPart.SurfaceGui.TextLabel

-- How long players have to cross the bridge
local timerDuration = 5
local timerStarted= false

local function startTimer()
    print("Countdown started")
	timerActive = true
	bridge.Transparency = 0
	bridge.CanCollide = true

	-- For loop that counts down from timerDuration
	for count = timerDuration, 0, -1 do
		timerText.Text = count
		wait(1)
	end

	-- Make the bridge not walkable
	bridge.Transparency = 0.8
	bridge.CanCollide = false
	timerText.Text = ""
	timerActive = false

end

local function buttonPressed (partTouched)
	local character = partTouched.Parent
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	print("part touched")
	
	if humanoid and timerActive == false then
		print("starting timer")
		startTimer()
	end
end

button.Touched:Connect(buttonPressed)