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.
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:
[ Pet Zoo! - Roblox]([CLOUDS] Pet Zoo 🦄 - Roblox)
So I am hoping to get some help replicating that in some way.
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: https://devforum.roblox.com/t/using-deltatime/417353
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.
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).
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
end)
if succes then
print("[LAST LOGIN] Successfully retreived " .. plr.Name .. " infomration") -- let us know the information was grabbed
print(info) --print the info
end
--[[ 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
end)
game.Players.PlayerRemoving:Connect(function(plr)
local succes, info = pcall(function()
LastLogin:UpdateAsync(plr.UserId .. "LastLogin", os.time()) -- Save the information
end)
if succes then
print("[LAST LOGIN] Successfully saved " .. plr.Name .. " infomration") --Log that the infomration was saves successfully
print(info) -- Print the information saved
end
end)
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.
It does not work man just tested out the recent script. Says something about the arithmetic (sub) on line 41, apparently the info is nil which is unable to be subtracted
That is likely because it was the first time using the data store so login time(I am assuming) was nil but after playing once it shouldn’t give that error
local Datastore = game:GetService("DataStoreService")
local LastLogin = Datastore:GetDataStore("LastLogin")
game.Players.PlayerAdded:Connect(function(plr)
local data
local succes, info = pcall(function()
data = LastLogin:GetAsync(plr.UserId.."LastLogin")
end)
if succes then
local TimeSinceLastSession = os.time() - data
local days = math.floor(TimeSinceLastSession/86400)
local hours = math.floor((TimeSinceLastSession % 86400)/3600)
local minutes = math.floor(((TimeSinceLastSession % 86400) % 3600)/60)
local seconds = math.floor((((TimeSinceLastSession % 86400) % 3600)%60)/1)
print(string.format("%id %02ih %02im %02is", days, hours, minutes, seconds).." passed since last login")
else
warn("Error gaining data: "..plr.UserId.." "..info)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local succes, info = pcall(function()
LastLogin:SetAsync(plr.UserId.."LastLogin", os.time())
end)
if not succes then
warn("Error saving data: "..plr.UserId.." "..os.time().." "..info)
end
end)
I would recommend learning about ProfileService way more efficient way of savings data then after you could use the ProfileService to store the last time a player connected to the game. I used it on my daily reward system