This is a link onto another thread, that got somewhere, but never got a final working answer. This what I have got so far. Basic description -
Look at the data table; this is the default data table, which contains all the default settings for a new player who joins the game. Now the problem arises when I add in say another class. All the code below it should have been able to add in the array but it doesn’t. If I add in just variable (for example Test) and give it a single value, and player rejoins, they have that value.
Basic idea is I want players to be able to join, get this data table, and then it should load up their saved table, and make edits to the deafult one depending what a players stats were. But for some reason, it won’t pick up new Arrays, only variables. There are now errors in Output. Can just copy this script, chuck it in game with Studio API enabled and HTTPServices Enabled. And can test there. The Scout class has been commented out. This is just an example of if in the future I want to add in more classes, I can, but until the code recognises that a new array has been added, I can’t do anything.
local data = {
Classes = {
['Knight'] = {
['ID'] = 1,
['Owned'] = true,
['Weapons'] = {
'Classic Sword',
},
['Armours'] = {
'Knight Armour'
},
['Trails'] = {
'None'
},
},
['Archer'] = {
['ID'] = 2,
['Owned'] = true,
['Weapons'] = {
'Bow and Arrow'
},
['Armours'] = {
'Archer Armour'
},
['Trails'] = {
'None'
},
},
--[[
['Scout'] = {
['ID'] = 3,
['Owned'] = false,
['Weapons'] = {
'Dagger'
},
['Armours'] = {
'Scout Armour'
},
['Trails'] = {
'None'
},
},
]]
},
ClassEquips = {
EquippedKnight = {
Weapon = 'Classic Sword',
Armour = 'Knight Armour',
Trail = 'None'
},
EquippedArcher = {
Weapon = 'Bow and Arrow',
Armour = 'Archer Armour',
Trail = 'None'
},
EquippedScout = {
Weapon = 'Dagger',
Armour = 'Scout Armour',
Trail = 'None'
},
},
EquippedClass = 'Knight',
Level = 1,
Exp = 0,
Gold = 100,
Gems = 0,
}
local httpService = game:GetService("HttpService")
local playerDataStore = game:GetService("DataStoreService"):GetDataStore("test002")
local playersData = {}
game.Players.PlayerAdded:Connect(function(player)
playersData[player.UserId] = {}
local function addressTable(receivedValue)
local function produceTable(fromValue)
local tableToReturn = {}
for key, value in pairs(receivedValue) do -- yes
tableToReturn[key] = addressTable(value)
end
return tableToReturn
end
return type(receivedValue) == "table" and produceTable(receivedValue) or receivedValue
end
for k, v in pairs(data) do -- assuming "data" is your prefab, changed that part for you
playersData[player.UserId][k] = addressTable(v) -- yes
end
local loadJSON = playerDataStore:GetAsync(player.UserId)
local setData = (loadJSON and httpService:JSONDecode(loadJSON)) or nil
-- Data part
if setData then
for key, value in pairs(setData) do
if playersData[player.UserId][key] then
playersData[player.UserId][key] = addressTable(value)
end
end
end
local user = playersData[player.UserId]
end)
game.Players.PlayerRemoving:Connect(function (player)
local saveJSON = httpService:JSONEncode(playersData[player.UserId])
playerDataStore:SetAsync(player.UserId, saveJSON)
end)
This thread contains some more useful information if you need more of an idea of how I was guided to this point