How do I make this script cleaner?

I’m writing a Intermission count down script right now, but I don’t want an entire line of code for every time the number goes down by one. How do I clean this up?

local GUIS = game.StarterGui
local IntermissionGUI = GUIS.Intermission

IntermissionGUI.text = "Intermission: 15"
IntermissionGUI.text = "Intermission: 14"
IntermissionGUI.text = "Intermission: 13"
IntermissionGUI.text = "Intermission: 12"
IntermissionGUI.text = "Intermission: 11"
IntermissionGUI.text = "Intermission: 10"
-- Etc.
3 Likes
local GUIS = game.StarterGui
local IntermissionGUI = GUIS.Intermission
local StartingNumber = 15

for i = 1,StartingNumber do
    IntermissionGUI.text = "Intermission: "..StartingNumber
    wait(1)
end

-- Etc.

Loop that repeats the ammount of time you are counting down :slight_smile:

3 Likes

doesnt loop bring lag to server???

1 Like

I don’t think it would cause lag?

1 Like

This is wrong. The loop you’ve given will count up from 1 to 15 starting from 1. Additionally, the text will be ‘Intermission: 15’ for all 15 iterations.

What you actually want is the following:

for i = StartingNumber, 0, -1 do
    IntermissionGUI.Text = "Intermission: "..i
    wait(1)
end

This will start i off at 15, and set the text to Intermission: value of i in each iteration, reducing i by 1 every iteration. So the first iteration will be ‘Intermission: 15’, the next will be ‘Intermission: 14’ and so on.

Performance is dependent on what code is run. If you run performance heavy code in a loop, sure, a loop on it’s own running some basic code will not.

4 Likes

or just

local StartingNumber = 15

for i = 1,StartingNumber do
    IntermissionGUI.text = "Intermission: "..StartingNumber - i
    wait(1)
end

1 Like

Yeah my bad I messed up on the last part you did it right tho :+1:

It doesn’t count down, it just says “Intermission: 15”

What does the UI look like in your game? Because judging by the variables you have:

local GUIS = game.StarterGui
local IntermissionGUI = GUIS.Intermission

It seems like you will need a ScreenGui inside of StarterGui, then a TextLabel inside of that ScreenGui. Because you are running this in a localscript, you shouldn’t be referring to GUIS as StarterGui because you are not updating it for the player at that point. You will need to get the LocalPlayer and then get their PlayerGui.

image

To change the text of a TextLabel, you need to refer to the property ‘Text’ (case sensitive)

local GUIS = game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
local IntermissionGUI = GUIS.Intermission
local StartingNumber = 15

for i = StartingNumber, 0, -1 do
	IntermissionGUI.Text = "Intermission: "..i
	wait(1)
end
3 Likes

I think your script is working. It’s probably something else I have in the script that I need to fix. Thanks.

1 Like