How can i update the players data store?

I’m having a problem that every time I add a new value to the players’ data store, the players that already had data saved before do not receive this new value, so I wanted to know a way for the players whenever I put some new value in the data store to receive this value and not lose the ones they already had

Just if your guys want to know this is the script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerData")
local DataLoad = game.ReplicatedStorage.Remotes:WaitForChild("DataLoad")

local StarterData = {
	Gold = 250,
	Level = 1,
	Energy = 0,
	EXP = 0,
	Gems = 0,
	EXPNeeded = 125,
	EnergyAddons = 1
}

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

	local sucess, PlayerData = pcall(function()
		return DataStore:GetAsync(plr.UserId) or StarterData
	end)

	if sucess then
		local leaderstats = Instance.new("Folder")
		leaderstats.Name = "leaderstats"
		leaderstats.Parent = plr

		local hidestats = Instance.new("Folder")
		hidestats.Name = "hidestats"
		hidestats.Parent = plr


		for Index, Value in pairs(PlayerData) do
			if Index ~= "EXPNeeded" and Index ~= "EXP" and Index ~= "EnergyAddons" then
				local NumberValue = Instance.new("IntValue")
				NumberValue.Parent = leaderstats
				NumberValue.Name = Index
				NumberValue.Value = Value
			else
				local NumberValue = Instance.new("IntValue")
				NumberValue.Parent = hidestats
				NumberValue.Name = Index
				NumberValue.Value = Value
			end
		end
		
		task.wait(1)
		DataLoad:FireClient(plr)
		
		hidestats.EXP.Changed:Connect(function()
			local expDefault = 125
			local expNeeded = expDefault * leaderstats.Level.Value
			if hidestats.EXP.Value >= expNeeded then
				hidestats.EXP.Value = 0
				hidestats.EXPNeeded.Value = expNeeded
				leaderstats.Level.Value = leaderstats.Level.Value + 1
			end
		end)
	end

end)


game.Players.PlayerRemoving:Connect(function(plr)
	local PlayerData = {

	}

	for Index, Child in pairs(plr.leaderstats:GetChildren()and plr.hidestats:GetChildren()) do
		if Child:IsA("IntValue") then
			PlayerData[Child.Name] = Child.Value
		end
	end

	pcall(function()
		DataStore:SetAsync(plr.UserId, PlayerData)
	end)

end)

After this, you should loop over the default table for the players and add any missing values:

for index, value in StarterData do
   if PlayerData[index] == nil then -- explicitly check if the value is nil (doesn't exist)
       PlayerData[index] = value -- add the missing key to the player's data
   end
end
1 Like

it works but now the data inst saving anymore

Make sure you also use game:BindToClose; under some circumstances Players.PlayerRemoving won’t fire

game:BindToClose(function()
   -- ... save all players data
end)

I recommend having the functionality of the save system as a separate function so that it can easily be re-used without copy and pasting:

local function save_data(player)
   -- ... save the player's data
end

game:BindToClose(function()
  for _, p in game:GetService("Players"):GetPlayers() do -- loop through all the players in the server
    task.spawn(save_data, p) -- calls the save data function
  end
end)
1 Like

still dont working, i will put some warns to see where is the fail

so after some warns i see the function save_data inst beggining called

now i made this beggining called and still dont working :skull:

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