Player's data erase when i make update or when i close the server

Hi! when i close all servers data previously saved loss, also if i make an update in PlayerData table how do i add it to other player’s, is there a way to “merge” old player data with new?? lets say i want to add more “wheelChairs” and i need to update a new “wheelChair” how do i add it to current player data so he can buy and save that new wheelchair in to his inventory?? I show my script called “Leaderboard” in server script service below:

local DataStore2 = require(game.ServerScriptService:WaitForChild(“DataStore2”)) – --require(etcetc)
–Cuando desee remover datos cambio la clave desde aca
local MainKey = “hereismainkey”

DataStore2.Combine(MainKey, “Lvl”, “Exp”, “Cash”, “Gems”, “Inventory”)

–if something is changed on this table
–change MainKey for something else
–to avoid problems.

–any starter player will get this table to hold current data.
local function CreateDataTable()
local PlayerData = {
Lvl = 1,
Exp = 0,
Cash = 0,
Gems = 0,
Inventory = {

		--upgradeable items
		[1] = {
			--shops
			-- each index [1]  represents the
			-- player inventory data related to a particular shop.
			[1] = {
				wheelChair = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
				keyboard = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
				healLaser = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			},
			[2] = {
				wheelChair = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
				keyboard = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
				healLaser = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			}
		},
		--current upgradeable item equipped
		[2] = {
				
				wheelChair = {shopLvl = 1, itemIndex = 1}, --when here i define wheelchair
				keyboard = {shopLvl = 1, itemIndex = 1}, --keyboard
				healLaser = {shopLvl = 1, itemIndex = 1}  --healLaser

		},
		--normal item you can save pets/vehicles/clothes(hats/pants/jackets) here
		[3] = {
			pets = {},
			vehicles = {},
			clothes = {
				hats = {},
				shirts = {},
				pants = {},
				shoes = {}
			}
			
		},
		--here we save favorite items
		--using table.insert
		[4] = {
			--we sort by item name and favourite index
			tools = {
				
			},
			vehicles = {
				
			},
			pets = {
				
			},
			clothes = {
				
			}
			
		}

	}
}

return PlayerData -- Cuando la funcion es llamada retorna la tabla

end

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

local PlayerData = DataStore2(MainKey, player):Get(CreateDataTable()) --GetPlayersDataTable

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


--Nivel
local Level = Instance.new("IntValue") --Creating a intvalue and parenting it to Leaderstats
Level.Name = "Level" --Name

--local cash = Instance.new("IntValue", leaderstats)
local cash = Instance.new("IntValue")
cash.Name = "Cash"

--Experiencia
local Gems = Instance.new("IntValue") --Creating a intvalue and parenting it to Leaderstats
Gems.Name = "Gems" --Name


local LvlData = DataStore2("Lvl", player)
local CashData = DataStore2("Cash", player)
local GemsData = DataStore2("Gems", player)

local inventoryData = DataStore2("Inventory", player)

--local inventory

--update leaderboard stats
local function UpdateAllStats(UpdatedStats)
	Level.Value = DataStore2(MainKey, player):Get(UpdatedStats).Lvl
	cash.Value = DataStore2(MainKey, player):Get(UpdatedStats).Cash
	Gems.Value = DataStore2(MainKey, player):Get(UpdatedStats).Gems
	--inventory = DataStore2(MainKey, player):Get(UpdatedStats)["Inventory"]

end


--player data 
UpdateAllStats(PlayerData)

DataStore2(MainKey, player):OnUpdate(UpdateAllStats)

--save status in leaderstats
leaderstats.Parent = player
Level.Parent = leaderstats
cash.Parent = leaderstats
Gems.Parent = leaderstats

end)

thx i really apreaciatte your help.

:Get() doesn’t set a value to the datastore it gets the value the thing in the brackets of get would be the default value if there is nothing inside. You should use :Set for your updateallstats function and also datastore2 has issues with shutting down, which you can fix with a bindtoclose function for when the game closes, however datastore2 automatically does save data on the player leaving

1 Like

thx, also if i change player table, lets say i add new items how do i store his new items on a redesigned table without losing his new data, idk if i express it good in english lol if you can help me with that it will be the solution done xd

This would probably be a lot more easier if you had a more generalized data structure, regardless that’s not really causing your problem.

Ideally what you could do is add the wheelchair to the players inventory and then invoke DataStore2’s Set method to replicate the change to the datastore.

https://kampfkarren.github.io/Roblox/api/#datastoreset

1 Like

Uh there isn’t an easy way to do that but Datastore2 has a :GetTable system which means you can set loads of values into one key and it automatically saves as a table. If you need help hit me up on discord Alfie#1772 if you have it

1 Like