Hello! Today we are going to be creating a Live Countdown across all servers. Without further a do, lets get to it!
About Epoch
Epoch is the amount of time that has passed since January 1st, 1970. We use Epoch to tell our code how much time is left. Epoch is extremely useful, and you might use it a lot in the future.
Scripting!
the First variable, will be our time that we want the Countdown to hit 0! Go to https://www.epochconverter.com/ to convert to your time! For this example, Im going to make the countdown, countdown to December 25th, 2020. (Christmas). Here is my input:
Make sure that its set to Local Time and not GMT. And after setting it to Local time, just insert the date! Now that you know how to get your date, lets begin coding.
local StartTime = 1608879600
The variable is the epoch that we want it to Countdown to! And now, we want to add another variable. That variable, will be Tween Service. This is totally up to you, but if you want a cool fade away effect once the timer hits 0, you might want to add this variable for reference! Like So:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
Okay, now that we have our basic variables, inside a Server Script inside of Workspace, we will begin designing our Countdown! Here is a basic countdown Model Link I Created: https://www.roblox.com/library/6048982565/Countdown-Model
Once you grab that, Insert it into your game, and put it into Workspace. Your workspace should look something like this:
Now, you can simply drag the Script into the Countdown. Like so:
Wonderful! Now, we can continue with our code!
Open up the script, and now we can create the Loop that counts down! Inside your script, add a simple While loop. Like this:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
end
Now, we have to add a wait(1) Inside our loop. This will make it wait 1 second before updating the countdown! Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
wait(1)
end
Simple. Now, we will create a Variable that is the current time epoch! Make sure this variable is inside the Loop. Why inside the loop? Because if its outside the loop… It will only tell us the current time when the script runs, and not every second like we want it to. So simply use os.time() to tell the current epoch time! Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
wait(1)
end
Cool, huh? Now, we will code the part that has the script check if the currentTime is equal to StartTime. And if it is, then we will tell the script what to do. (What happens once it hits 0.)
We will do this by using a simple if statement. Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
end
wait(1)
end
Simple, but works well! Now, to test out if everything is in order, we will put an else inside the if statement. So if the countdown isn’t done counting down to 0, then it will print how much time is left!
First, lets make a Variable Inside the loop. This will be how much time is left! We will subtract the StartTime variable, to the currentTime variable. Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
end
wait(1)
end
Now, we will add the else inside the if statement, with the print! The else is checking if the countdown is NOT done counting down, then it will print how much time is left. Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
else
print(timeLeft)
end
wait(1)
end
And if you check the Output, it will print how much time (in seconds) is left until your chosen epoch time!
Now, we will begin making the actual countdown text change.
We will need to use a LOT of big brain math to determine how many days that are left, hours that are left, ect. First, lets a variable for math.Floor! We use math.Floor for the big brain math. Add the variable like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
local mFloor = math.floor
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
else
print(timeLeft)
end
wait(1)
end
Epic. Now, we will add the big brian math. This will divide the epoch until it gets the days, hours, minutes, and seconds. Add the big brain math like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
local mFloor = math.floor
local DaysLeft = mFloor((timeLeft / 60 / 60 / 24) % (365 + 0.2425))
local HoursLeft = mFloor((timeLeft / 60 / 60) % 24)
local MinutesLeft = mFloor((timeLeft / 60) % 60)
local SecondsLeft = mFloor(timeLeft % 60)
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
else
print(timeLeft)
end
wait(1)
end
Isnt that some awesome math? Now… We will make the Timer Text update! We will simply get the decendants of the Billboard GUI. And then we will check if it has the className of, “Frame” Then if it does, then we will set its child’s text depending on the Frames Name! We will also make sure that it is inside the else statement. We will do this Like so:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
local mFloor = math.floor
local DaysLeft = mFloor((timeLeft / 60 / 60 / 24) % (365 + 0.2425))
local HoursLeft = mFloor((timeLeft / 60 / 60) % 24)
local MinutesLeft = mFloor((timeLeft / 60) % 60)
local SecondsLeft = mFloor(timeLeft % 60)
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
else
print(timeLeft)
local decendants = script.Parent.BillboardGui:GetDescendants()
for i,v in pairs(decendants) do
if v.ClassName == "Frame" then
if v.Name == "Days" then
v.Number.Text = DaysLeft
end
if v.Name == "Hours" then
v.Number.Text = HoursLeft
end
if v.Name == "Minutes" then
v.Number.Text = MinutesLeft
end
if v.Name == "Seconds" then
v.Number.Text = SecondsLeft
end
end
end
end
wait(1)
end
Cool huh? Now if you give it a test, it will display how many days, hours, minutes and seconds are left until your chosen time! Now as promised, I will teach a simple fade System. We will use Tween Service to tween the TextTransparency. We will also use Get Decendants here! We will make it fade once the timer hits 0. So we will put it inside the if statement. Like So:
local StartTime = 1608879600
local TweenService = game:GetService("TweenService")
while true do
local currentTime = os.time()
local timeLeft = StartTime - currentTime
local mFloor = math.floor
local DaysLeft = mFloor((timeLeft / 60 / 60 / 24) % (365 + 0.2425))
local HoursLeft = mFloor((timeLeft / 60 / 60) % 24)
local MinutesLeft = mFloor((timeLeft / 60) % 60)
local SecondsLeft = mFloor(timeLeft % 60)
if currentTime >= StartTime then
-- Whatever is inside here is what happens when the countdown hits 0! --
local dec = script.Parent.BillboardGui:GetDescendants()
for i,v in pairs(dec) do
if v.ClassName == "TextLabel" then
TweenService:Create(v,TweenInfo.new(2,Enum.EasingStyle.Linear),{TextTransparency = 1}):Play()
TweenService:Create(v,TweenInfo.new(2,Enum.EasingStyle.Linear),{TextStrokeTransparency = 1}):Play()
end
end
break
else
print(timeLeft)
local decendants = script.Parent.BillboardGui:GetDescendants()
for i,v in pairs(decendants) do
if v.ClassName == "Frame" then
if v.Name == "Days" then
v.Number.Text = DaysLeft
end
if v.Name == "Hours" then
v.Number.Text = HoursLeft
end
if v.Name == "Minutes" then
v.Number.Text = MinutesLeft
end
if v.Name == "Seconds" then
v.Number.Text = SecondsLeft
end
end
end
end
wait(1)
end
And boom, a Countdown has been created! If you want the sources for learning more about Tween Service, Epoch, and strings for the 0’s in front of single digits, then here are the links!
https://developer.roblox.com/en-us/api-reference/class/Tween
https://developer.roblox.com/en-us/api-reference/lua-docs/os
If you notice anything wrong, please tell me by leaving a Reply, or messaging me! Thank you.