Is there a way to save a player's login in one's own game?

Hello everyone,
my plan is to create a GUI with information about when a specific player was last online. Now, my question is, is this even possible, and if so, how?

I haven’t found anything about it, unfortunately. :frowning:

4 Likes

Sure this is possible. One way could be to get the time when the player logs out via os.time() and safe it with an DataStore.

1 Like

There has to be, but not sure how.
There’s a game called Check a User's Last Online Information [+ More!] - Roblox where you can just type in any user’s name and it’ll give you that stat.

2 Likes

Yeah right if he want to look when the user was lost online on the Roblox platform he could use the presence api

Yeah it is!
You can save that information using a datastore.

-- ENABLE API SERVICES!!

local DS  = game:GetService("DataStoreService"):GetDataStore("Last_Online_Datastore")
local Players = game:GetService("Players")

function getLastOnline(playerID)
	local data
	local Success, failed_dataretrieve = pcall(function()
		data = DS:GetAsync(playerID)	
	end)
	if(typeof(data) == "number")then
	if(Success)then
		-- Convert to date
		local formatter = os.date("!*t", data) -- This is the unix timestamp !!
		local date
		local Formatted,Failed_toFormat = pcall(function()
		date = tostring("Hour: "..formatter.hour.." Day: "..formatter.day.." Month: "..formatter.month.." Year: "..formatter.year)
		end)
		if(Formatted)then
			print("Last online of user "..Players:GetNameFromUserIdAsync(playerID).." was: "..date)
		else
			print("Failed to format.")
		end
		end
	else
		print("Data is not number(?) / Player data not available.")
	end
end

Players.PlayerAdded:Connect(function(player)
	-- Check for a players lastonline
	getLastOnline(player.UserId)
	
	-- Set last online now, because they joined
	local Update, failed_update = pcall(function()
		DS:SetAsync(player.UserId, os.time())	
	end)
	
	if(failed_update)then
		warn("Failed to update data!")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	-- Set last online now, because they are leaving
	local Update, failed_update = pcall(function()
		DS:SetAsync(player.UserId, os.time())	
	end)
	
	if(failed_update)then
		warn("Failed to update data!")
	end
	
end)

Here we set a last online whenever a player joins / leaves and it all gets stored in a datastore.
In the function: getLastOnline you MUST give as a parameter a player USERID and it will retrieve their data, if they already played!

Hope this helps.

1 Like