How to save points even when leave

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

whats that?
bind to close?
can you tell me what is it and where and how

See this

Make sure to use pcall as it is safer to use and helps prevent crashing. Many people tend to not use it, but it is recomended.

it is hard for me to understand and use the scripts that are mentioned in an article in the developer hub. The thing that I want to save is the file “leaderstats” which inside any player that joins the game, and the values inside the file, “Points” and “XP”


game:BindToClose(function() 
	for _, player in pairs(game.Players:GetPlayers()) do 
       local points = player.Points
       local success, errorMsg = pcall(function()
       pointsDS:SetAsync(plr.UserId, points.Value
       end 
end)

this script is full of errors just check the script before you tell me to use it, when pasted your script, too many red lines.
red lines = error

and there was 2 unknown globals

Instead of saving data with the bind to close, just add a wait(5) so that the PlayerRemoving function can run, saving the player’s data. There’s not much use in saving within the BindToClose()

Putting the data into a table reduces the amount of things to save to just one. So, it saves space, you can save data more often, and there is “less” data to remember.

can you show me the results of the script?

First: Please learn from this script, do NOT past the script.
Second: This script is an adjustment for Nifemiplayz script. It works for me.

I think that if your having a hard time understanding those articles you might have to learn some of the basics of LuaU(the coding language Roblox uses)
I recommend if you wanna learn about saving player data follow this article which teaches about data stores in a way easier to understand and shows some code examples found on the developer wiki and also just following the basic coding tutorials found on the Roblox developer onboarding page

I hope this helps!!!

He posted that assuming you would setup the data store and where the points are saved yourself, that script is just how to save it to a datastore not setup the data store I recommend following the article/pages I linked on my other post. I’ll quote it below

1 Like

Try this script.


local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"

	local Points = Instance.new('NumberValue', leaderstats)
	Points.Name = "Points"
	Points.Value = 0

	local value1Data = Points

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
	end)

	if s then
		Points.Value = value1Data --setting data if its success
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Points.Value) --setting data
	end)
	if not s then game:GetService("TestService"):Error(e) 
	end
end)

1 Like