Saving leaderstats value when a client leaves

Hello! I am having some issues to save leaderstats value. For some reason, it fails to save.

local Players = game:GetService("Players")
local LS = Instance.new("Folder")
local DS = game:GetService("DataStoreService")
local DataStore = DS:GetDataStore("DataStore1")


Players.PlayerAdded:Connect(function(plr)
	local LS = Instance.new("Folder")
	LS.Name = "leaderstats"
	LS.Parent = plr
	local Level = Instance.new("IntValue")
	Level.Parent = LS
	Level.Name = "Level"
	Level.Value = DataStore:GetAsync(plr.UserId)
	pcall(function(success, errorMessage)
		DataStore:SetAsync(tostring(plr.UserId), {
			Level.Value
		}, plr.UserId)
		if success then 
			print("Called succesfully!")
		else 
			warn(errorMessage)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(plr)
	local LS = plr:FindFirstChild("leaderstats")
	pcall(function(success, errorMessage)
		DataStore:SetAsync(plr.UserId, {
			LS.Level.Value
		}, plr.UserId)
		if success then 
			print(success)
		else
			print(errorMessage)
		end
	end)
end)
2 Likes

Does it fail always or sometimes?

1 Like

It hasnt saved all the times I have tested it

1 Like

DataStore:SetAsync(tostring(plr.UserId), {Level.Value}, plr.UserId)

you have 2 plr.UserId . Is it supposed to be like this? Because my code sample only have 1

local PlayerDataStoreId = "player_".. player.UserId
local PlayerStatsData = {
		Money = player.leaderstats.Money.Value,
		Level = player.leaderstats.Level.Value,
		XP = player.leaderstats.XP.Value
	}
PlayerStats:SetAsync(PlayerDataStoreId,PlayerStatsData)
1 Like

Mhm, it supposedly eases GPRD requests
(I am not sure how though)

docs:

Table of UserIds, highly recommended to assist with GDPR tracking/removal.
Default Value: “{}”

i think you need to insert userid’s with a table

You can easily erase those through :RemoveAsync(), no need for whoever told you to do that.

1 Like

Hello . Just a reminder, there is no use for tostring(plr.UserId) . btw , is there any errors when loading? Please provide more information

When a user wants their data deleted, Roblox will most likely be able to do this automatically with that third parameter(probably)

1 Like

I don’t think so, Roblox supposedly doesn’t access any game

Not really any error in that script

Man . That is a really messy script . Anyways I have fix some parts for you . Hope it works lol
PS im on mobile :skull:

local Players = game:GetService("Players")
local LS = Instance.new("Folder")
local DS = game:GetService("DataStoreService")
local DataStore = DS:GetDataStore("DataStore1")


Players.PlayerAdded:Connect(function(plr)
	local LS = Instance.new("Folder")
	LS.Name = "leaderstats"
	LS.Parent = plr
	
	local Level = Instance.new("IntValue")
	Level.Parent = LS
	Level.Name = "Level"
	Level.Value = 0 -- Set it to level for new players
	
	local Data = DataStore:GetAsync(plr.UserId) -- Data would look something like {Level = x} since this is how we set it when the player leave
	
	if Data then --check if the player have a valid data
		if Data.Level then--Just an extra check to prevent errors if the player does not have a saved value for any reason
			Level = Data.Level
		end
	end
	
	--//There is no need for this line since the data save when the player is leaving anyways
	--pcall(function(success, errorMessage)
	--	DataStore:SetAsync(tostring(plr.UserId), {
	--		Level.Value
	--	}, plr.UserId)
	--	if success then 
	--		print("Called succesfully!")
	--	else 
	--		warn(errorMessage)
	--	end
	--end)
end)

Players.PlayerRemoving:Connect(function(plr)
	local LS = plr:FindFirstChild("leaderstats")
	
	local suc ,err = pcall(function()
		local SaveData = {}
		SaveData.Level = LS.Level.Value -- SaveData.Level basically set the index(assume it is like a name of a property) to the value of "leaderstats.Level.Value"
		DataStore:SetAsync(plr.UserId,SaveData,plr.UserId)
	end)
	if err then
		warn(err)
	end
end)

basically some errors that might happen is that if the player have no saved data, the data would be nil and setting a value to nil will throw an error . Another problem there is that when he player leave, you set the value to a table {} . Setting value to a table will also throw an error

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.