Points not saving up

Greetings. I have made a simple script for my game to save up points, but for a reason it won’t save them. I have tried some others scripts, but they also didn’t work. The script that I firstly created was:

    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)

Any help would be really appreciated.

1 Like

There are some problems with this code.

You should not save data directly from a leaderstat, because players can modify them. Keep your actual points value saved in a table in your script, so only the server has the real value.

Http calls should be wrapped in a pcall, because they can fail.

local success, failMessage = pcall(function()
--your datastore call
end)

Finally, you should probably use UpdateAsync, it’s preferred over SetAsync. (see wiki for reason why)

iirc, data stores do not work within Roblox Studio unless you explicitly enable them. Try testing in a real server. Also, are any errors printed to output?

1 Like

this should save data, what’s the error?

Are you sure after saving data, you retrieve it?

  local players = game:GetService("Players")
  players.PlayerAdded:Connect(function(player)
 
  local ID = "Points-"..player.UserId
  local pointsValue = DataStore:GetAsync(ID)

  local points = player.leaderstats.Points
  points.Value = pointsValue or 0
  -- ^ you could do this while creating the instance

  end)

remember to use protected calls for DataStore requests

1 Like

Wait, I didn’t retrieve it. I just tried it, it worked. Also, I didn’t really see an error.

Thank you for your feedback, it’s really appreciated. I’ll also check the UpdateAsync.