Script is not working

I want to want to make a timer bridge

when i run the code it is not working idk why?

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)

You declared timerActive only in the startTimer() function. So, it is never false, it is either nil before the timer is started, or true after the timer is started, change timerActive to timerStarted, in both the startTimer function and the buttonPressed handler.

edit: you must change it to timerStarted because you already intialised timerStarted as false at the start of the script. But you use timerActive for the rest of the code, which is why timerActive is never false, but either nil or true.

1 Like