How do I make a time played leaderboard that SAVES Data?

Alright so, I want to make a leaderboard which passes by minutes & want that time to be saved once the player leaves the game. I’ve checked youtube & looked up on the roblox toolbox but nothing actually helped.

If anyone can help me figure out how I can have a leaderboard and add time by minute and keep that time saved in a datastore could be amazing!

Apologies if I’m in the wrong category on posting this. I need help on how to figure this out, new to scripting :sweat_smile:

8 Likes

Try to look here to see how to set up a datastore: Data Stores | Documentation - Roblox Creator Hub

Your objective is very simple, in order to actually save it, you’ll need a datastore. Try to set one up based on the tutorial. If it doesn’t work, reply and provide your code.

3 Likes

Like WooleyWool said you need to strore it in data stores
I made an example!
Here it is
The code waits 60 seconds to increase the counter

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("TotalTime")

local Players = game:GetService("Players")

local sessionData = {}

local AlwaysTrue = true

-- Function to save player's data

local function savePlayerData(playerUserId, plr)

playerData:SetAsync(playerUserId, plr.leaderstats.Time.Value)

print("Saved!")

end

-- Function to save player data on exit

local function saveOnExit(player)

local playerUserId = "Player_" .. player.UserId

savePlayerData(playerUserId, player)

end

local function onPlayerAdded(plr)

local playerUserId = "Player_" .. plr.UserId

local leaderstats = Instance.new("Folder")

leaderstats.Parent = plr

leaderstats.Name = 'leaderstats'

local timee = Instance.new("IntValue")

timee.Parent = leaderstats

timee.Name = 'Time'

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("TotalTime")

local playerUserId = "Player_" .. plr.UserId

local sessionData = {}

local data = playerData:GetAsync(playerUserId)

timee.Value = data

repeat

wait(60)

local newTime = timee.Value + 1

timee.Value = newTime

until AlwaysTrue == false

end

Players.PlayerRemoving:Connect(saveOnExit)

Players.PlayerAdded:Connect(onPlayerAdded)
21 Likes

Hey thank you very much for that! 1 question, how do I make it as if it counts by 60 seconds. Basically marks a number after a minute?

The script i gave you waits 60 seconds before increasing the value so it waits a minute

1 Like

Alright thank you! Sorry I ran into a problem, where do I place the script & do I need to change the script’s name after I place it into my game?

You place it in ServerScriptService and you can name it whatever

1 Like

Do I need to have API on for it to work?

No, It only uses Roblox stuff, If it used any API you needed to install it would have a require()

I’m confused, the data saves when I have the API on. When I don’t have the API, the data doesn’t save.

NVM NVM, it works without the API.

You’re not supposed to give scripts on the DevForum.

As I know, DevForum is a place to help people with scripting problems. He/She can give scripts if he/she doesn’t know how to do script something.

4 Likes

Yes you need API. DataStoreService is possible with API on.

It says “Help and Feedback” , you’re clearly allowed to give a script or help someone with their scripts…smh.

2 Likes

Will this script break other leaderboard scripts too? I was thinking to use it for my Obby game which has Stage leaderboard {Checkpoints} scripted…