01 | --List of Map Selections |
02 | local maps = ( "DownwardSpiral" ) |
03 | local boardbasic = game.ServerStorage [ "HoverBoardBasic" ] |
04 | local STGUI = game.StarterGui.SystemGUI.Frame.Label |
05 | --Message and Map Randomizer |
06 | DownwardSpiral = game.Lighting.maps.DownwardSpiral |
07 | --Timer-- |
08 | local timer = game.ReplicatedStorage [ "Time" ] |
09 | --Main Game Script-- |
10 | while true do |
11 | STGUI.Text = "Loading..." |
12 | print ( "Loaded" ) |
13 | wait( 5 ) |
14 | game.workspace.IntermissionMusic:Play() |
15 | STGUI.Text = "Intermission-" ..timer.Value |
16 | print ( "Displayed" ) |
17 | wait( 60 ) |
18 | game.workspace.IntermissionMusic:Stop() |
19 | end |
I'm creating a countdown timer for my game, and I don't want to write 122 lines of useless messaging code. I'm using a StringValue in the ServerScriptStorage and I set it to a number. I'm trying to make it display on the GUI counting down. It displays, but any method I tried didn't make it countdown. Here is my progress system for the game, and I write it as I go. My question is what code should I use to make it count down, and where to put it? Should I put it in a separate script or in the main?
View the hierarchy here. The actual GUI size, color, etc. can be anything you like. Also, the name of the scripts don't matter, so they can be anything you want.
Make sure you have FilteringEnabled set to true.
"ServerScript"
01 | local timeValue = game.ReplicatedStorage:WaitForChild( "Timer" ) |
02 | local timerActive = false |
03 |
04 | -- YOUR CODE HERE (MOST LIKELY RANDOM FUNCTIONS, ETC.) -- |
05 |
06 | while true do -- Infinite loop |
07 | -- YOUR CODE HERE (PRE-TIMER) -- |
08 | timerActive = true |
09 | timeValue.Value = 60 -- The time it starts with. |
10 |
11 | while timerActive do |
12 | wait( 1 ) |
13 |
14 | timeValue.Value = timeValue.Value - 1 |
15 |
"LocalGui"
01 | local gui = script.Parent |
02 | local label = gui:WaitForChild( "Background" ):WaitForChild( "TimerLabel" ) |
03 | local timeValue = game.ReplicatedStorage:WaitForChild( "Timer" ) |
04 |
05 | function formatTime(input) |
06 | local output |
07 |
08 | if type (input) ~ = "number" or input < 0 then -- Makes sure it's a number, OR if it's a number and it's a negative number. |
09 | output = "--:--" |
10 | else |
11 | local formatString = "%i:%.2i" -- This is just a formatting string. |
12 | local seconds = math.floor(input % 60 ) -- input % (modulo) 60 gets the remainder of the division of input and 60. Ex: input = 65; 65 % 60 is 5 |
13 | local minutes = math.floor(input / 60 ) -- math.floor rounds numbers down towards negative infinity. Just in case input / 60 is a decimal, it becomes a whole number. |
14 |
15 | output = formatString:format(minutes, seconds) |
The -- YOUR CODE HERE --
is where your code should go.
I hope this helped. If you have any questions, feel free to contact me or just post a comment on this answer.
I also noticed that you created a table incorrectly on your first line. It should be curly braces ({}
), not parenthesis (()
).
You could use this for the countdown. It would show the time remaining(i) until it reaches 0 and would end.
1 | for i = 60 , 0 ,- 1 do --Sets the timer to 60 seconds, counting down by 1 until it reaches 0 |
2 | wait( 1 ) |
3 | STGUI.Text = "Intermission-" ..i |
4 | end |