Time Spent Leaderboard

I am trying to create a leader board that shows how long a user has played the game globally. Is there any scripts that can help create this? I do not want to create a leader board for how long someones been in the server. I am having trouble creating a script like this since there is no tutorials. Most leader board scripts are for wins, KO’s, etc.

This is a example of a model I found but has no script for it:

1 Like

Look at this video:

I watched that but I have little skill in scripting. Do you know how to convert the script into Time Spent on game globally.

No I don’t but you could always just make a table/object to store the value of how long the player has spent on your game. It’s value could go up every second.

All you need to do is track how long the player has been in the server, and update the script to reflect this change. This would require a separate script that would increment the Wins intValue (you’ll probably rename this to “TimeSpent” or something) in each player’s leaderstats folder. Something like…

while wait(1) do
	for _, player in pairs(game.Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")
		local TimeSpent = leaderstats:FindFirstChild("TimeSpent")
		if TimeSpent then
			TimeSpent.Value = TimeSpent.Value + 1
		end
	end
end

…might work out. Then, you would change it so that the Ordered Datastore will update with the time spent.

WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.TimeSpent.Value)

Make sure to change the first script which initializes the player’s leaderstats folder and subsequent intValue so that the name of the intValue in the folder is “TimeSpent” or whatever you want to name it instead of “Wins.” However, I recommend not using ObjectValues and instead create a table, but the above method is the simplest protocol for your request.

3 Likes

Thank you. This will help. I appreciate it.

I would say don’t even use that while loop method of tracking time, it’s resource intensive just for the sake of being resource intensive.

When the player joins you can collect the timestamp, i.e:

game.Players.PlayerAdded:Connect(function(player)
    timeStamp = os.time()
end)

And when they leave you can perform a basic equation to calculate the play time.

game.Players.PlayerRemoving:Connect(function(player)
   local playTime = os.time() - timeStamp
end)

Saves up using a while loop and makes it fairly simple.

6 Likes

Thank you I appreciate your help. :slightly_smiling_face: