I’m not a very advanced scripter and I tried making a countdown to event timer with a UI that shows the time left but for some reason the time left displayed looks really off.
I’m not sure what’s wrong in this code, could someone help clarify what I need to fix?
heres an image of what the countdown displayed:
and heres the code to the countdown:
local timeToStart = 1714532400 --epoch time
local timeToStop = 1714575600 --epoch time
local daysLeft = 0
local hoursLeft = 0
local minsLeft = 0
local secsLeft = 0
function liveEvent()
--code for the live event here
end
while wait(1) do
local timeNow = os.time()
if timeNow >= timeToStop then --stops checking if its time to start the event
break
end
if timeNow >= timeToStart then
print("Start the Live Event")
liveEvent()
break -- stops the loop
else
daysLeft = math.floor(timeToStart-timeNow / 86400)
temp = (timeToStart-timeNow % 86400)
hoursLeft = math.floor(temp / 3600)
temp = (temp % 3600)
minsLeft = math.floor(temp / 60)
temp = (temp % 60)
secsLeft = math.floor(temp)
script.Parent.SurfaceGui.Time.TimeLeft.Text = ((daysLeft).." Days, "..(hoursLeft).." Hours, "..(minsLeft).." Mins, "..(secsLeft).." Secs.")
end
end
The time calculation math is wrong. Try the corrected version:
local timeToStart = 1714532400 -- Epoch time for when to start
local timeToStop = 1714575600 -- Epoch time for when to stop
local daysLeft = 0
local hoursLeft = 0
local minsLeft = 0
local secsLeft = 0
function liveEvent()
-- Code for the live event here
print("Live Event is running!")
end
while wait(1) do
local timeNow = os.time()
if timeNow >= timeToStop then
break -- Stops checking; the event time has passed
end
if timeNow >= timeToStart then
print("Start the Live Event")
liveEvent()
break -- Stops the loop; event has started
else
-- Corrected calculation for time left
local timeDiff = timeToStart - timeNow
daysLeft = math.floor(timeDiff / 86400)
local temp = timeDiff % 86400
hoursLeft = math.floor(temp / 3600)
temp = temp % 3600
minsLeft = math.floor(temp / 60)
secsLeft = temp % 60 -- Direct calculation without math.floor as remainder will be seconds left
-- Ensure your script's parent has a SurfaceGui with a TextLabel named TimeLeft for this to work
if script.Parent:FindFirstChild("SurfaceGui") and script.Parent.SurfaceGui:FindFirstChild("Time") then
script.Parent.SurfaceGui.Time.TimeLeft.Text = (daysLeft.." Days, "..hoursLeft.." Hours, "..minsLeft.." Mins, "..secsLeft.." Secs.")
end
end
end