How would I find if a player has been playing since a certain time?

SO in the latest Bubble Gum Simulator update I saw that there was a title called OG Bubbler and you needed to be playing since Christmas 2018 to have the title.

So I was wondering, how could I do this?

If I used a data store saying what time the player started playing it could work but I want to be at like New Years 2020 (my game is fairly new).

For example, this code:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("TimeFirstJoined")

game.Players.PlayerAdded:Connect(function(plr)
	local data
	local success, message = pcall(function()
		dataStore:GetAsync(plr.UserId)
	end)
	
	if success and data then
		-- nothing happens, player has been in the game before
	elseif not (data) or not (success) then -- no data or no success
		print("New data probably")
		local Time = os.date("!*t") -- finding the time the player first joined
		dataStore:SetAsync(plr.UserId, Time) -- saving the time the player first joined
	end
end)

I tried looking at the Developer Hub and the DevForum, but I couldn’t find anything.

Check this: How do I get the Hour and Minute using os.date? - #11 by Minstrix I think this post can help you.

If I made it that when the player joins, and the player doesn’t have any data, then it saves the time, then the player could get a title for being here since Summer 2020 instead of New Years 2020.

This will not work (at least I don’t think)

You’d have to make a new datastore, and when the player joins the game and there is no entry for that player in the datastore, you’d create one for the player and save the time they’ve joined using os.time:

local joinTimeDataStore = game:GetService("DataStoreService"):GetDataStore("Join")

game.Players.PlayerAdded:Connect(function(player)
    local joinTime = joinTimeDataStore:GetAsync(player.UserId)

    if not joinTime then
        joinTimeDataStore:SetAsync(player.UserId, os.time())
    else
        print((player.Name .. " first joined the game at %d seconds"):format(joinTime))
    end
end)

If you want to check if the player joined during Summer 2020, you’ll have to compare the start and end of summer with the time they joined:

local START = os.time {year = 2020; month = 6; day = 1}
local END = os.time {year = 2020; month = 9; day = 21}

if joinTime >= START and joinTime <= END then
    print("player joined during Summer 2020")
end
1 Like

Just clarifying, are you supposed to use these {squiggly parentheses}, not these (parentheses)?

os.time {} is the same as os.time( {} ); If you put a literal right beside a function, it’ll call the function with the literal beside it as an argument

If you have a string next to a print, it’ll print the string literal beside it:

print "hello" -- prints hello in the output
-- above is the same as print("hello")

If you have a table next to unpack and print it, it’ll print all of its elements:

print( unpack {1, 2, 3} ) -- prints 1, 2, 3
-- above is the same as print(unpack({1, 2, 3}))

Much like for os.time, the same thing applies:

print( os.time {year = 2020; month = 6; day = 1} ) -- prints the seconds until Summer 2020 from the UNIX Epoch
-- above is the same as print( os.time( {year = 2020; month = 6; day = 1} ) )
1 Like