(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