So for my leaderboard script I want to give the top 3 players a reward every week. I don’t know how to do this however (using os.time()).
Here is my current script:
local dss = game:GetService("DataStoreService")
local dataStore = dss:GetOrderedDataStore("TotalCashTest1")
local crystalsDataStore = dss:GetOrderedDataStore("TotalCrystalsTest1")
local function update()
local success, errorMessage = pcall(function()
local Data = dataStore:GetSortedAsync(false, 5)
local crystalData = crystalsDataStore:GetSortedAsync(false, 5)
local crystalPage = crystalData:GetCurrentPage()
local page = Data:GetCurrentPage()
for Rank, data in ipairs(page) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local val = data.value
local isOnLeaderboard = false
for i, v in pairs(workspace.TotalCashLeaderboard.SurfaceGui.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if val and isOnLeaderboard == false then
local newFrame = game.ReplicatedStorage:WaitForChild("Template"):Clone()
newFrame.Player.Text = Name
newFrame.Rebirths.Text = val
newFrame.Rank.Text = "#"..Rank
newFrame.Position = UDim2.new(0,0,newFrame.Position.Y.Scale + (0.8 * #workspace.TotalCashLeaderboard.SurfaceGui.Holder:GetChildren()),0)
newFrame.Parent = workspace.TotalCashLeaderboard.SurfaceGui.Holder
end
end
for CrystalsRank, dataCrystals in ipairs(crystalPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(dataCrystals.key))
local Name = userName
local val = dataCrystals.value
local isOnLeaderboard = false
for i, v in pairs(workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder:GetChildren()) do
if v.Player.Text == Name then
isOnLeaderboard = true
break
end
end
if val and isOnLeaderboard == false then
local newFrame = game.ReplicatedStorage:WaitForChild("Template"):Clone()
newFrame.Player.Text = Name
newFrame.Rebirths.Text = val
newFrame.Rank.Text = "#"..CrystalsRank
newFrame.Position = UDim2.new(0,0,newFrame.Position.Y.Scale + (0.8 * #workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder:GetChildren()),0)
newFrame.Parent = workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder
end
end
end)
if not success then
warn(errorMessage)
end
end
while true do
wait(10)
for _, plr in pairs(game.Players:GetPlayers()) do
dataStore:SetAsync(plr.UserId, plr.TotalStats:WaitForChild("TotalCash").Value)
crystalsDataStore:SetAsync(plr.UserId, plr.TotalStats:WaitForChild("TotalCrystals").Value)
end
for _, frame in pairs(workspace.TotalCashLeaderboard.SurfaceGui.Holder:GetChildren()) do
frame:Destroy()
end
for _, crystalFrame in pairs(workspace.TotalCrystalsLeaderboard.SurfaceGui.Holder:GetChildren()) do
crystalFrame:Destroy()
end
update()
print("UPDATED")
wait(10)
end
Now would I use os.date or os.time?
If you use os.date() you can check the month and then see if the date % 4 == 0. (This should in theory work. Haven’t tested it.)
You could use os.date()
. Here is a useful article that outlines it pretty well.
local DateTable = os.date("*t", os.time()) -- Get all the information about the current date in local time
if DateTable.wday == 1 then
print("Today is Sunday in local time!")
end
If you are making a weekly leaderboard you can use the DateTable.wday
to check which day of the week it is. Then, each time the day is the same every week, update the leaderboard.
[wday is] the current weekday starting from Sunday
So when DateTable.wday == 3
, the day will be Tuesday in local time. I would suggest you use global time, however, which you can get by doing:
-- !*t instead of *t returns UTC time.
local DateTable = os.date("!*t", os.time())
Hope this helps 
2 Likes
Thanks, and also how would I do something that happens every 30 minutes (and starts ON THE DOT)?
I tried using tick() like @Zephyr_42 suggested to me but that didn’t go really well.
I think you would only be able to do this with a running server. What you can do is when the server starts up, calculate the amount of time from now until the next half hour / hour. Then wait that interval. After that, simply add a wait(1800)
seconds (half an hour) in your code.
-- Server Startup script
local DateTable = os.date("*t", os.time())
-- The current number of seconds since the hour started
local CurrentSec = DateTable.min * 60 + DateTable.sec
-- The number of seconds in half an hour
local HalfHourSec = 60 * 30 -- (1800 seconds)
-- The time between now and the next half hour, in seconds.
local TimeToWait = HalfHourSec - CurrentSec
-- If the time is past the first half hour, go to the hour (e.g. if it is 16:47 or 15:52)
if TimeToWait < 0 then
TimeToWait = 2 * HalfHourSec - CurrentSec
end
-- Align to nearest future half hour
wait(TimeToWait)
while true do
-- code
wait(HalfHourSec)
end
1 Like
Thanks again, and also (sorry for so many questions!) how would I find out if the date is May 16 (I know how to do the if statement, but I want to know like if the date isn’t May 16 then you use the TimeToWait thing and then stuff like that)?
Would I do:
local currentSecDayThing = DateTable.min * 60 * 24 + DateTable.sec -- instead of the hour it is the day
Also for the math here:
is it
TimeToWait = 2 * (HalfHourSec - CurrentSec)
--or
TimeToWait = (2 * HalfHourSec) - CurrentSec
Also when I test it out with like 1 minute, the wait time is negative so it starts right away.
You can use the DateTable like so:
if DateTable.day == 16 and DateTable.month == 5 then
-- the date is 16th of may
else
-- the date is not the 16th of may
end
Lua follows the rules of BIDMAS (sometimes called PEDMAS, BEDMAS, BODMAS, PIDMAS, etc.), which stands for Brackets, Indices, Division, Multiplication, Addition and Subtraction. This is the order in which mathematical operations take place. Therefore, TimeToWait = 2 * HalfHourSec - CurrentSec
would do the multiplication first, then the subtraction, and so is equivalent to TimeToWait = (2 * HalfHourSec) - CurrentSec
1 Like
But if it isn’t may and it’s like 11:58 pm and the player plays into May 16, the drop parties won’t stop because the if statement only runs once.
If I were to do that, then would I do:
local TimeToWait = os.time({year = 2020; month = 5; day = 16}) - os.time()
print(TimeToWait) -- May 16 is 11836800, and if os.time() = 11834800, then the output would be 2000