What do you want to achieve? I’m trying to script a countdown, that begins if another .Text value isn’t equal to “0/5” (meaning it starts whenever the .Text value changes to 1/5 and higher). I also want to make it start at 30 seconds and count down to 0. If the countdown ended (is at 0), then it will change the text to “Starting the game…”.
What is the issue? It doesn’t really work, from what I tested it seems like it counts down only when I’m loading, not loaded yet. Because when I once took 3 seconds to load, the number was 27 and then it froze. When I loaded for 1 second, the number was 29.
What solutions have you tried so far? I searched up topics at the Developer Forum, and even tried searching YouTube, but nothing really helped me.
This is the code I made:
local StartingIn1 = game.StarterGui.Lobby1Queue.Frame.StartingIn
local PlayerCount1 = game.StarterGui.Lobby1Queue.Frame.PlayerCount
while true do
if PlayerCount1.Text ~= "0/5" then
for i = 30, 1, -1 do
StartingIn1.Text = "Starting in: "..i.." seconds"
wait(1)
if i == 0 then
StartingIn1.Text = "Starting the game..."
end
end
end
end
You do have a script which changes PlayerCount1.Text to “1/5” when you join, right? If not,
When you are loading, the text is probably “nil” so the script goes.
Once you are finished loading, the text returns to “0/5” and stops loading
First of all, you don’t have a wait in that loop, meaning that your playtest session will instantly crash. Secondly, the countdown won’t activate until the PlayerCount1 text isn’t “0/5”, so you’ll need an external Script/LocalScript to change the PlayerCount's text.
Here’s a better script I devised for you. It utilizes practices that you may not be familiar with but it should work. All you need to do is define the variables at the top and create a NumberValue and define players as that. The script will run when you change the value of players.
local players = ... -- NumberValue
local playersLabel = ...
local statuslabel = ...
local con
local function count()
con = task.defer(function()
for i in 30, 1, -1 do
statuslabel.Text = ("Starting in %i seconds"):format(i)
task.wait(1)
end
statuslabel.Text = "Starting the game..."
end)
end
players:GetPropertyChangedSignal("Value"):Connect(function()
playersLabel.Text = ("%i/5"):format(players.Value)
if players.Value <= 0 then
if con == nil then return end
task.cancel(con)
con = nil
return
end
count()
end)
Then yes, upon joining, your text will say “nil” for a few seconds before saying “0/5”
You could try disabling the script, and parenting another script under this script which waits like 2 seconds before enabling it.
And do add a wait() like @voozy_v said unless you want your game to die