Hello, good day. I’m working on a timed button and i’ve encountered a problem. The code is originally from education.roblox.com but i’ve made some revisions to make the timer be on a UI instead on a part. The timer doesn’t show up on the gui but there’s no errors showing up on the Output tab.
The Code
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.StarterGui.ScreenGui
-- Gets the Text that will display the timer
local timerText = timerPart.TextLabel
-- How long players have to cross the bridge
local timerDuration = 5
local timerStarted= false
local function startTimer()
print("Countdown started")
timerStarted = 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 = ""
timerStarted= false
end
local function buttonPressed (partTouched)
local character = partTouched.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid and timerStarted == false then
startTimer()
end
end
button.Touched:Connect(buttonPressed)
I think it has to do with the fact that you reference StarterGui. Try using player.PlayerGui instead.
Once a player plays a game, everything in StarterGui gets cloned in to a folder inside the player called ‘PlayerGui’. So while your script does probably work, the player isn’t going to see the changes.
I always do it this way:
local player = game.Players:WaitForChild(partTouched.Parent.Name) local PlayerGui = player:WaitForChild(“PlayerGui”)
Edit: I forgot this was a normal script. You have to get the player from the partTouched.
It looks like this script is on the server (child of the bridge object) so you will need to get the Player who touched the button and show the timer on his/her PlayerGui
local bridge = script.Parent
-- Gets the button as it's typed in the Explorer
local button = game.Workspace.ButtonBridge
-- How long players have to cross the bridge
local timerDuration = 5
local timerStarted= false
local players=game:GetService("Players")
local function startTimer(player)
print("Countdown started")
-- Gets the part for the display
local timerPart = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui")
-- Gets the Text that will display the timer
local timerText = timerPart.TextLabel
timerStarted = 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 = ""
timerStarted= false
end
local function buttonPressed (partTouched)
local character = partTouched.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
local player=players:GetPlayerFromCharacter(character)
if humanoid and timerStarted == false then
startTimer(player)
end
end
button.Touched:Connect(buttonPressed)
Yes, you should see the text label before the button is touched unless the gui isn’t enabled or the label is not set to visible.
I just tested the code I added above and it works ok here