How would I use os.date() and then see who's been in the game the past week?

Hi! I’m thinking bout how I would use an os.time() or os.date() to check if it’s a specific number of a month. It will then check who’s been in the game for the past week.

How would I go about to check who’s been in the game for the past week? I just can’t think about how I would do that.

*(Currently, there is no scripting as I am just brainstorming. I’ve tried looking through some of the wiki to grab everyone but not sure?)

I would use DataStores to save data for every player that joined in the current week.Not sure if this is the best solution, but this is what I could come up with:

  • Since there are 604,800 seconds in one week, we can use math.floor(os.time()/604800) to get the current week in a number.

  • This week number can be used as a DataStore key, so every week a new key is generated to store data for every player that joined in the current week.

We will be using ordered data stores. Heres the wiki page for that: DataStoreService | Documentation - Roblox Creator Hub

Here’s some example in code:

local DSS = game:GetService('DataStoreService')
local Players = game:GetService('Players')

Players.PlayerAdded:connect(function(plr)
   local CurrentWeek = math.floor(os.time()/604800) --Get the current week number
   DSS:GetOrderedDataStore(CurrentWeek):SetAsync(plr.UserId, os.time()) --Save the current time in the players dataStore of the current week.
end)

Since we stored the player’s join time in an ordered dataStore, we can get a list of the players that joined in a specific week, sorted from most recently to least recently joined.

Here is how we do this

local DSS = game:GetService('DataStoreService')
--So after player's date is saved from the last script, we can load the entire ordered data store as an array 
--If we want to get the current week we do this:
local CurrentWeek = math.floor(os.time()/604800)

--and now we can use this current week as the DataStore key to get the list of players who joined this week!
local CurrentWeekData = DSS:GetOrderedDataStore(CurrentWeek):GetSortedAsync()
local CurrentWeekPlayers= CurrentWeekData:GetCurrentPage(false, 10) --10 is the page size
--This will give an array of the last 10 joined players of this week

--to print all the players, just do this:
for _, pJoined in pairs(CurrentWeekPlayers) do
   print(pJoined.key) -- This will print the user id of the player
   print(game.Players:GetNameFromUserIdAsync(pJoined.key)) -- This will get the username of the player from their UserID
end

Hope this helps to solve your question! :slight_smile:

1 Like

Hey! It seems like that would work however I did run into a slight problem.

I need an argument given on line 7 / “local CurrentWeekData”

I don’t know how this datastore stuff works so I decided I’d just ask you?

oh yeah, you’re right.

these paremeters (false, 10) should go in line 7 in the GetSortedAsync() function instead of GetCurrentPage

so the last script becomes:

local DSS = game:GetService('DataStoreService')
--So after player's date is saved from the last script, we can load the entire ordered data store as an array 
--If we want to get the current week we do this:
local CurrentWeek = math.floor(os.time()/604800)

--and now we can use this current week as the DataStore key to get the list of players who joined this week!
local CurrentWeekData = DSS:GetOrderedDataStore(CurrentWeek):GetSortedAsync(false, 10) --10 is the page size
local CurrentWeekPlayers= CurrentWeekData:GetCurrentPage() 
--This will give an array of the last 10 joined players of this week

--to print all the players, just do this:
for _, pJoined in pairs(CurrentWeekPlayers) do
   print(pJoined.key) -- This will print the user id of the player
   print(game.Players:GetNameFromUserIdAsync(pJoined.key)) -- This will get the username of the player from their UserID
end
3 Likes