How do I add a new Datastore Table value to already existing one without overwriting data?

(Reposted for issues, old topic deleted)

Hi it’s me again, so basically I have datastore tables that have characters info stored in, and everything works normally no problem.

The actual issue here is, since the game will get updates eventually, meaning new characters, how do I add a new character to the datastore table without overwriting old stuff?

Example:

There’s 2 characters in the game, and the datastore gets created with those 2 if it doesnt exist.
However if it already exists, when I add a new value/character, it won’t create it because the datastore already exists with pre-saved old values

So the question is, how do I make it insert a new table or value at a specific position inside another already-existing datastore table without resetting data?

Something like: detecting a new table in the standard preset, and add it to existing data if it doesn’t exist.

Here’s the code (if something looks weird, all values are intentional. It works as intended I just need help on how to add more when needed with future updates):

local DSS = game:GetService("DataStoreService") -- Service
local Store = DSS:GetDataStore("characterData") -- DataStore

local Storage = {} -- Stores Player Data
local Key = "Player_" -- Key to get data

game.Players.PlayerAdded:Connect(function(player)
	local Success, Data = pcall(function() -- protected call
		return Store:GetAsync(Key..player.UserId) -- returns Data if Player has any
	end)

	if Success then -- if Accessed DataStores
		if Data then -- If Player has Data
			Storage[player.UserId] = Data -- Applies Table data to Custom Player Data
		else --If Player has no Data
			Storage[player.UserId] = {
				characters = {
					FighterOne= {
						Attacks = {
							Attack1 = 1,
							Attack2 = 2,
							Attack3 = 3,
							Attack4 = 4,
							Attack5 = 5,
							Attack6 = 6
						},
						currentSkin = "Standard"
					},
					FighterTwo= {
						Attacks = {
							Attack1 = 1,
							Attack2 = 2,
							Attack3 = 3,
							Attack4 = 4,
							Attack5 = 5,
							Attack6 = 6
						},
						currentSkin = "Standard"
					},
				}	
			}
		end
	else
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Success = pcall(function()
		Store:SetAsync(Key..player.UserId, Storage[player.UserId]) -- Saves Player Table
	end)

	if Success then
		print("Player Left, Data Saved.")
	else
		print("Player left but Data Saving Failed.")
	end


end)

Edit: Forgot Player Removing function, in case the entire code is needed

1 Like

table.insert() maybe? I don’t really get what your saying

I am aware on how to insert something new to a table

What I mean is how can I have a NEW value inserted without resetting

If a datastore is created like you see there, it will create a specific data to edit and take from, however I need to know a way to DETECT if a new datastore exists (meaning new character was added), and add the new character table (in this case we’d call it FighterTHREE) to the already existing datastore of a player who joined before it got added, with that being unable to have it in the datastore without the game adding it in afterwards in some way.

If there’s only 2 fighters and I add a 3rd one later on, the players who joined before the new character wouldn’t have its table in the datastore, I need to know how to get them to save it aswell without resetting/overwriting their data

After the table is set to the pre-saved value or the default ones, check if the new value(s) exist in it, if not, add them.

1 Like

I know, but you just gave me a better idea.

1 Like

Well, say your idea so others can learn from it later on and your welcome.

The new idea had failed so I decided to use yours instead, and it worked, so whoever reads this should use the one marked as solution

1 Like

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