Hey, I'm trying to make a script that counts down from 20 to 1 and displays it on a text label, but I can't seem to get it working. I printed the text to the output and it counts down just as it should, but the text label always stays at "Game Starting in 20...".
Here is the script, which is located in ServerScriptService:
label = game.StarterGui.Screen.Label while true do x = true t = 20 while x == true do label.Text = "Game Starting in "..t.."..." print(label.Text) t = t - 1 if t == 0 then x = false end wait(1) end end
Help would be appreciated, -Spacetimit
Well, there should be local
to define label as a variable
. I don't think not adding local
will work with a variable
anymore. Anyways, create a localscript
under the TextLabel
. Then add local
to your variable to define it as one. Change the variable to fit the new parents and children. The adjusted script is attached below.
local label = script.Parent local x local t while true do x = true t = 20 while x == true do label.Text = "Game Starting in "..t.."..." print(label.Text) t = t - 1 if t == 0 then x = false end wait(1) end end
Some notes for you to take whenever working with Gui's.
- Always use
LocalScript
(s) when working with Gui's- Always use
local
when defining a variable (good practice!)
The label is updating, just not for every player. For this, you'd use PlayerGui
and, preferably, a LocalScript.
StarterGui is the descendant of the DataModel (game) that holds GUI objects, local scripts, etc. that are replicated to a player's PlayerGui. Both can be updated in real-time, but only one will display its changes to the player in real-time. When you're changing GUI values, or doing anything ScreenGui-related, you should always use the PlayerGui
.