How to save points even when leave

So I made a game. The game is basically just 2 parts. and the leaderboard has “Points” And “Xp” and it is obvious what do those 2 parts do. They give you points AND ALSO XP. But if I leave and rejoin, they are not saved.

The script that I used to make the leaderstats was a script that waits for the player until they join, creates a new leaderstats file, and the values. But I want a script that not just creates a new file and values, but also saves them even when the player leaves.

1 Like

https://developer.roblox.com/en-us/articles/Data-store

it’s hard for me to understand those articles but let me see

no, i couldn’t understand what to do

There is actually a Roblox service that helps accomplish this very thing! The Roblox DataStore service is a service that allows you to save data at various called points throughout the game. At the bottom is a link to more about it. To save every value of the player’s leader stats (“Points” and “Xp”), you could create an empty dictionary and iterate over the values of your leader stats while adding them to the dictionary to be saved (This also makes it more convenient if you want to add more in future).

Example of this would be:

local playerData = {}
for each,stat in pairs(player.leaderstats:GetChildren()) do
	playerData[stat.Name] = stat.Value
end

You could then update a datastore with this data when the player leaves and load it back when a player rejoins the game.

Here is a video for help if it’s still confusing:
https://www.youtube.com/watch?v=hWhr8ntzq5M&t=764s

1 Like

If you read the article, you can see a small preview of a few scripts that will help you.

Players.PlayerAdded
Get data

game:BindToClose(), Players.PlayerRemoving & every x minutes (interval)
Save data

and what’s better than a video?I am going to watch it and I will probably understand

Read this tutorial. It help me to create my own save system.

pairs are mainly used for dictionaries, use ipairs for arrays such as :GetChildren().

So like game.Players.leaderstats.Points:ipairs ?
oh, how do i save the data

Thanks for pointing that out! I thought I wrote something off in my script. Couldn’t tell where :sweat_smile:

1 Like

This is the documentation for pairs and ipairs:
https://education.roblox.com/en-us/resources/pairs-and-ipairs-intro

It essentially is a way to iterate over a dictionary or list. Think of it as looping not a function.

From the source itself:
Numeric for: Programming in Lua : 4.3.4
Generic for: Programming in Lua : 4.3.5

You should use the video I posted to understand better. It accomplishes exactly what you are asking. Don’t just watch it, follow along in your code with it so you can learn better. It’s a lot easier to understand something when you can troubleshoot and think over it.

Like it was long video, long script.

You would use DatastoreService like so:

local DSS = game:GetService("DatastoreService") -- Gets service
local pointsDS = DSS:GetDataStore("points") --Creates data store using the name in parameters

game.Players.PlayerAdded:Connect(function(player) 
    local points = Instance.new("IntValue", player) --Instancing a value into the player, you could do this in a folder if you want it organized
    points.Value = pointsDS:GetAsync(plr.UserId) or 0--The value of the Instance will try to find the saved value in the DataStore if it doesn't then the value will be false
end)

game.Players.PlayerRemoving:Connect(function(player)
    local points = player.Points --Getting the value from our player

    local success, errorMsg = pcall(function() --If you don't know what pcalling is consider reading about it in Roblox DevHub
        pointsDS:SetAsync(plr.UserId)
    end)

    if errormessage then
       print("Error occured while saving data!")
    end
end)

I don’t know if its a personal preference but i would save them to individual datastores instead of putting them in a table

that script didn’t work at all.

In Studio try BindToClose and activate the APIs

Looks like this should be

pointsDS:SetAsync(plr.UserId, points.Value)

So the datastore is actually setting a key value pair