How can I find how long it's been since a player plays my game last?

  1. What do you want to achieve?
    Hello everyone! I’ve been poking around with some smaller projects lately as I recently got a PC so my tools for development have been expanded a lot more. The small project I am working on currently is a recreation of the original Cookie Clicker, designed to be more faithful to the original browser game compared to other remakes seen on Roblox. What I would like to achieve, but need some help with, is detecting how long it’s been since a player joined the game last so I can provide an offline reward to players when they next join.

  2. What is the issue?
    So, my issue is that I am unsure how to exactly do this. I have some lesser experience with stuff such as os.time() making a daily reward system in the past, but I don’t know how I would get the length of time that a player isn’t in the game for. I know this can be done because there is offline reward collection for this game:
    [:dog: Pet Zoo! :paw_prints: - Roblox](
    So I am hoping to get some help replicating that in some way.

  3. What solutions have you tried so far?
    As I don’t know how I would attempt to try this, I have looked to see if I can get some help searching for similar topics. I believe DeltaTime can be used as it get’s elapsed time but I am not sure how I can apply it. While I am familiar with DeltaTime as well, this post here does a good job talking about it:
    Though this is helpful, I would like to apply this to find how long a player is “offline,” or not playing my game, which I am unsure about how to accomplish.

As a side note if there is anything specific that can be linked so I can read it and learn some more, I would be open to receiving anything so I know better for next time.

Restating my problem just to avoid any confusion: I would like to find out how I can get the amount of time that elapses from the last time a player leaves my game to the next time they join for use in rewarding a player based on how long they are offline.

1 Like

You can use Datastores to save the time when player left and get the length of how long it’s been since their last login. It’s pretty basic and i have made that before but I don’t remember what my script looks like (I’m on mobile currently).

Oh. I actually didn’t think of this as an option. I thought it would be more efficient using DeltaTime but I am going to look into this right now.

1 Like

Code pls fdsfdsfsfdsfsfsfsfdsfsd

Hey. To accomplish this you will need a two things. You are going to need datastores, and os.time().

os.time() will return the number of seconds since a specific date.

Datastores will then store this info when they leave, and retrieves it when the player joins the game.

I will supply you with code at a later time when I get on my PC.

1 Like

As promised here is the code. I have not tested it so I’m not entirely sure this works. However it should.

local Datastore = game:GetService("DataStoreService") -- Getting the Datastore Service
local LastLogin = Datastore:GetDataStore("LastLogin") -- Geeting the Datastore (you can change LastLogin to whatever you want

game.Players.PlayerAdded:Connect(function(plr) -- Runs when a player joins the game
	plr.CharacterAdded:Wait() -- Wait for the player's character to load
	local succes, info = pcall(function()
		LastLogin:GetAsync(plr.UserId .. "LastLogin") -- Grabbing the information from the last time they were online
	if succes then 
		print("[LAST LOGIN] Successfully retreived " .. plr.Name .. " infomration") -- let us know the information was grabbed
		print(info) --print the info
	--[[ to calculate the amount of time since they last played we need to use os.time()]]--
	local Login_time = os.time() --this give you the number of seconds since Jan 1st, 1970 00:00:00
	local TimeSinceLastSession = info - Login_time -- Getting the number of seconds between the sessions
	--[[If you want to calculate the number of minutes/hours between sessions you'd do the following]]--
	local Minutes = TimeSinceLastSession/60
	local Hours = Minutes/60
	local Days = Hours/24

	local succes, info = pcall(function()
		LastLogin:UpdateAsync(plr.UserId .. "LastLogin", os.time()) -- Save the information 

	if succes then 
		print("[LAST LOGIN] Successfully saved " .. plr.Name .. " infomration") --Log that the infomration was saves successfully
		print(info) -- Print the information saved
1 Like

Sorry I saw this so late. I have been quite busy with things and haven’t been on the dev forum recently. I appreciate the reply as this is exactly what I was looking for. Thanks a ton! I am going to use this with the hopes of teaching myself how to do it on my own.

No problems. Glad I could help!

1 Like