Help, my currency script is not saving the player's points

Hello everyone.
I’ve made a simple script to save the points that a player has. But, they are not being saved and I don’t understand why.

Here is the part of my code that saves the value of the points the player has.

game.Players.PlayerRemoving:Connect(function(player)
	local ID = pointsName .. "-" .. player.UserId -- pointsName = "Points"
	DataStore:SetAsync(ID, player.leaderstats.Points.Value) -- I also tried with player.leaderstats[pointsName].Value
	print(player.Name .. "'s points were saved.")
end)

Thanks in advance.
Regards, nanitook.

1 Like

Hello!

Are you getting any error messages?

I’ve found that some things get removed before that function is called, so you might try caching their points value and then just firing a function to save that cached value.

1 Like

Did you define “DataStore”?
30chars

Can we see your loading script? Perhaps it’s a problem in loading and it actually is being saved.

If you’re 100% sure it’s the saving part, then make sure you aren’t removing the value you’re saving before you save it.

No, I do not receive any type of error in the console :confused:

1 Like

Alright. How do you know it didn’t save it?

Sure:

local pointsName = "Points"
local DataStore = game:GetService("DataStoreService"):GetDataStore("PointsStore_db1")

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = pointsName
	points.Parent = folder
	
	local ID = pointsName.."-"..player.UserId
	local savedData = nil
	
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	
	if savedData ~= nil then
		points.Value = savedData
		print("Player currency data loaded." .. " | JNS |")
	else
		print("New player added to the currency system" .. " | JNS |")
	end
end)

You don’t seem to be defining Datastore anywhere. Or did you just not include it in the script on the forum?

He defines it just fine :stuck_out_tongue: (filler characters)

1 Like

I’d highly recommend looking into that caching method I was talking about in my previous post, because I have a feeling that the leaderstats is just getting deleted before the save can occur.

Because after changing my points, and reconnecting, the value returns to the previous one (the value before changing it).

Example:
I currently have 100 points. – I’ve put them before. And at that time they were saved.
I change them to 50 using the console in Studio.
I disconnect.
I reconnect and the value of the points is 100.

1 Like

Whoops had to scroll up in that window on my tablet. Thanks. :grinning:

2 Likes

No problem, it is a community after all, we’re all here to help.

2 Likes

Wait are studio APIs enabled under GameSettings?
Are you setting the data in the client (if accurate play solo is on)? Setting the data in the server is the only way to actually save the data.

1 Like

I’d also like to suggest, when using pcalls they by default return two values

local Success, Error = pcall(function() end)

Utilize that to know if things were successful, and if not print the Error

1 Like

I’d like to help you solve this issue so, would you mind giving me your ear for a few seconds and possibly trying something I am about to write?

1 Like

But if I stop saving player points when disconnected, I should update the DataStore every time the player points value changes. (When he buys / sells something). Because updating it for a certain time would not be entirely practical. I’m wrong?

That’s still not quite what I’m getting at, I’m saying you can cache their values and save it on exit. What I mean by this is storing values in a table when they’re updated, saving occasionally, and then saving the final cache value on exit.

1 Like

Yes, the Studio Access to API services is enabled.
And everything that has to do with the points, I handle it from the server side