This is my data table, that is given to everybody when they first join the game. It has all the default data, numbers, etc. That a beginning player should have
local data = {
Classes = {
Knight = {
ID = 1,
Owned = true,
Weapons = {
'Sword',
},
Armours = {
'Griswold',
},
Equipped = {
Weapon = 'Sword',
Armour = 'Griswold',
},
},
Archer = {
ID = 2,
Owned = true,
Weapons = {
'Bow',
},
Armours = {
'Iron',
},
Equipped = {
Weapon = 'Bow',
Armour = 'Iron',
},
},
Scout = {
ID = 3,
Owned = false,
Weapons = {
'Dagger',
},
Armours = {
'Finn',
},
Equipped = {
Weapon = 'Dagger',
Armour = 'Finn',
},
},
Hunter = {
ID = 4,
Owned = false,
Weapons = {
'Crossbow',
},
Armours = {
'Bombo',
},
Equipped = {
Weapon = 'Crossbow',
Armour = 'Bombo',
},
},
},
Trails = {
'None',
},
EquippedClass = 'Knight',
EquippedTrail = 'None',
Level = 1,
Exp = 0,
Gold = 0,
Gems = 0,
Games = 0,
Wins = 0,
Losses = 0,
Kills = 0,
Deaths = 0,
}
return data
Now, a problem I faced in the past and coming back again to be a problem, is when edits are made to the tables within the data. Changing the Gold, ID and Owned inside the classes, etc. all saved perfectly fine. However, when changes are made to a table inside it then they don’t load properly.
This is what I do to load in the data when a plyaer joins
function updateObject(old, new)
local update = {}
for i, v in next, new do
update[i] = (type(v) == 'table' and old[i] and updateObject(old[i], v)) or old[i] or v
end
return update
end
function dataManager:GetData(player)
local loadJSON = playerDataStore:GetAsync(player.UserId)
local setData = (loadJSON and httpService:JSONDecode(loadJSON)) or {}
playersData[player.UserId] = updateObject(setData, data)
dataReady = true
end
So the problem arises in this updateObject()
function. I had put this in a while back, because I was having problems of if a player joins, they get given that default data, but if I ADD a new section to the data, they will still load their old original data table. So the point of this updateObject()
function was to look for changes inside the default data and add them to the players previously saved data. However, this ends up reverting any tables in the function back to the default data.
The example being, if I was to add a weapon to the Knight class, so
table.insert(data.Classes.Knight.Weapons, 'New sword')
then it does add this new item to the data.Classes.Knight.Weapons
but this gets wiped when the updateObject()
function is called