Hello,
My friend and I have been working on the data loading for my game. It first loads the users saved data. If it is nil, it will load the default table I have defined, but for whatever reason, it doesn’t like going serializing my table. Here is the script: (I recommend copying it and putting it into a code editor so that you can minimize the default table. It is well over a thousand lines long.)
local http = game:GetService("HttpService")
local ds = game:GetService("DataStoreService")
local database = ds:GetDataStore("test-database2.7")
local players = game:GetService("Players")
-- Functions
function CreateInstance(instanceType: string, parent: Instance, properties: { string: any })
local function SetValue(object: Instance, property: string, value: any)
object[property] = value
end
local object = Instance.new(instanceType, parent)
for property, value in pairs(properties) do
local pass, err = pcall(SetValue, object, property, value)
if not pass then warn(err) end
end
return object
end
function DeserializeData(data, name: string, location: Instance): Instance
local deserializedFolder = CreateInstance("Configuration", location, { Name = name })
local function Create(list, parent: Instance)
--// Types
local types = {
["table"] = function(index: string, value: { any })
print(parent)
Create(
value[1],
CreateInstance("Configuration", parent, { Name = index })
)
end,
}
--// Main Loop
for index, value in pairs(list) do
local first, second = value[1], value[2]
local type = types[ second ]
if type then
type(index, value)
continue
end
CreateInstance(second, parent, { Name = index, Value = first })
end
end
Create(data, deserializedFolder)
return deserializedFolder
end
function SerializeData(deserializedData: Instance): { any }
local serializedData = {}
local function Serialize(parent: Instance, list: {})
for _, child: Instance in pairs(parent:GetChildren()) do
if table.find({ "Configuration", "Folder" }, child.ClassName) then
list[child.Name] = {}
Serialize(child, list[child.Name])
table.insert(list[child.Name], 2, "table")
continue
end
if not child:IsA("ValueBase") then continue end
list[child.Name] = { child.Value, child.ClassName }
end
end
Serialize(deserializedData, serializedData)
return serializedData
end
local function playerAdded(player)
local defaultTable = {["Citycoins"] = {[1] = 0,[2] = "NumberValue"},["Codes"] = {[2] = "table",["2k16 Here We Come"] = {[1] = false,[2] = "BoolValue"},["Gold Tacos"] = {[1] = false,[2] = "BoolValue"},["Han Solo"] = {[1] = false,[2] = "BoolValue"},["Meddling Kids"] = {[1] = false,[2] = "BoolValue"},["My Names Not Rick"] = {[1] = false,[2] = "BoolValue"}},["Construction"] = {[2] = "table",["Con1"] = {[1] = "",[2] = "StringValue"},["Con10"] = {[1] = "",[2] = "StringValue"},["Con11"] = {[1] = "",[2] = "StringValue"},["Con12"] = {[1] = "",[2] = "StringValue"},["Con13"] = {[1] = "",[2] = "StringValue"},["Con14"] = {[1] = "",[2] = "StringValue"},["Con15"] = {[1] = "",[2] = "StringValue"},["Con2"] = {[1] = "",[2] = "StringValue"},["Con3"] = {[1] = "",[2] = "StringValue"},["Con4"] = {[1] = "",[2] = "StringValue"},["Con5"] = {[1] = "",[2] = "StringValue"},["Con6"] = {[1] = "",[2] = "StringValue"},["Con7"] = {[1] = "",[2] = "StringValue"},["Con8"] = {[1] = "",[2] = "StringValue"},["Con9"] = {[1] = "",[2] = "StringValue"}},["Expansions"] = {[2] = "table",["Expansion1"] = {[1] = false,[2] = "BoolValue"},["Expansion2"] = {[1] = false,[2] = "BoolValue"},["Expansion3"] = {[1] = false,[2] = "BoolValue"},["Expansion4"] = {[1] = false,[2] = "BoolValue"},["Expansion5"] = {[1] = false,[2] = "BoolValue"},["Expansion6"] = {[1] = false,[2] = "BoolValue"}},["Inventory"] = {[2] = "table",["Beta1 Statue"] = {[1] = 0,[2] = "NumberValue"},["Beta2 Statue"] = {[1] = 0,[2] = "NumberValue"},["Mobile Home"] = {[1] = 0,[2] = "NumberValue"},["Pre-Beta Statue"] = {[1] = 0,[2] = "NumberValue"},["Prison"] = {[1] = 0,[2] = "NumberValue"},["Roblox HQ"] = {[1] = 0,[2] = "NumberValue"},["Taco Cart"] = {[1] = 0,[2] = "NumberValue"},["The Mystery Shack"] = {[1] = 0,[2] = "NumberValue"},["The Sanchez Residence"] = {[1] = 0,[2] = "NumberValue"},["The Walking Dead"] = {[1] = 0,[2] = "NumberValue"},["Trailer Park"] = {[1] = 0,[2] = "NumberValue"},["Vader Statue"] = {[1] = 0,[2] = "NumberValue"}},["InventoryItems"] = {[2] = "table",["5 Star Hotel"] = {[1] = 0,[2] = "NumberValue"},["Apartment"] = {[1] = 0,[2] = "NumberValue"},["Bank"] = {[1] = 0,[2] = "NumberValue"},["Basic Store"] = {[1] = 0,[2] = "NumberValue"},["Basketball Court"] = {[1] = 0,[2] = "NumberValue"},["Beta1 Statue"] = {[1] = 0,[2] = "NumberValue"},["Beta2 Statue"] = {[1] = 0,[2] = "NumberValue"},["BloxMart"] = {[1] = 0,[2] = "NumberValue"},["Bloxton Pizza"] = {[1] = 0,[2] = "NumberValue"},["Car Dealership"] = {[1] = 0,[2] = "NumberValue"},["Chemical Factory"] = {[1] = 0,[2] = "NumberValue"},["Condo"] = {[1] = 0,[2] = "NumberValue"},["Construction"] = {[1] = 0,[2] = "NumberValue"},["Death Star"] = {[1] = 0,[2] = "NumberValue"},["Empire State Building"] = {[1] = 0,[2] = "NumberValue"},["Factory"] = {[1] = 0,[2] = "NumberValue"},["Fast Food Coliseum"] = {[1] = 0,[2] = "NumberValue"},["Fountain"] = {[1] = 0,[2] = "NumberValue"},["Gas Station"] = {[1] = 0,[2] = "NumberValue"},["Gold Road 3Way"] = {[1] = 0,[2] = "NumberValue"},["Gold Road 4Way"] = {[1] = 0,[2] = "NumberValue"},["Gold Road Straight"] = {[1] = 0,[2] = "NumberValue"},["Gold Road Turn"] = {[1] = 0,[2] = "NumberValue"},["Golden Skyscraper"] = {[1] = 0,[2] = "NumberValue"},["Golden Taco Cart"] = {[1] = 0,[2] = "NumberValue"},["Highway Entry"] = {[1] = 0,[2] = "NumberValue"},["Highway Road"] = {[1] = 0,[2] = "NumberValue"},["Highway Straight"] = {[1] = 0,[2] = "NumberValue"},["Highway Turn"] = {[1] = 0,[2] = "NumberValue"},["Hospital"] = {[1] = 0,[2] = "NumberValue"},["Hotel"] = {[1] = 0,[2] = "NumberValue"},["House"] = {[1] = 0,[2] = "NumberValue"},["Juan Statue"] = {[1] = 0,[2] = "NumberValue"},["Lake"] = {[1] = 0,[2] = "NumberValue"},["Loft"] = {[1] = 0,[2] = "NumberValue"},["Logan Statue"] = {[1] = 0,[2] = "NumberValue"},["Luke Statue"] = {[1] = 0,[2] = "NumberValue"},["Mall"] = {[1] = 0,[2] = "NumberValue"},["Mansion"] = {[1] = 0,[2] = "NumberValue"},["McDonald"] = {[1] = 0,[2] = "NumberValue"},["Millennium Falcon"] = {[1] = 0,[2] = "NumberValue"},["Mobile Home"] = {[1] = 0,[2] = "NumberValue"},["Modern Skyscraper"] = {[1] = 0,[2] = "NumberValue"},["Movie Theater"] = {[1] = 0,[2] = "NumberValue"},["National Bank"] = {[1] = 0,[2] = "NumberValue"},["Night Club"] = {[1] = 0,[2] = "NumberValue"},["Park"] = {[1] = 0,[2] = "NumberValue"},["Pool"] = {[1] = 0,[2] = "NumberValue"},["Pre-Beta Statue"] = {[1] = 0,[2] = "NumberValue"},["Prison"] = {[1] = 0,[2] = "NumberValue"},["Retro Diner"] = {[1] = 0,[2] = "NumberValue"},["Road 3Way"] = {[1] = 0,[2] = "NumberValue"},["Road 4Way"] = {[1] = 0,[2] = "NumberValue"},["Road Dirt 3Way"] = {[1] = 0,[2] = "NumberValue"},["Road Dirt 4Way"] = {[1] = 0,[2] = "NumberValue"},["Road Dirt Straight"] = {[1] = 0,[2] = "NumberValue"},["Road Dirt Turn"] = {[1] = 0,[2] = "NumberValue"},["Road Straight"] = {[1] = 0,[2] = "NumberValue"},["Road Turn"] = {[1] = 0,[2] = "NumberValue"},["Roblox HQ"] = {[1] = 0,[2] = "NumberValue"},["Skate Park"] = {[1] = 0,[2] = "NumberValue"},["Skyscraper"] = {[1] = 0,[2] = "NumberValue"},["Taco Cart"] = {[1] = 0,[2] = "NumberValue"},["Tennis Court"] = {[1] = 0,[2] = "NumberValue"},["The Mystery Machine"] = {[1] = 0,[2] = "NumberValue"},["The Mystery Shack"] = {[1] = 0,[2] = "NumberValue"},["The Sanchez Residence"] = {[1] = 0,[2] = "NumberValue"},["The Walking Dead"] = {[1] = 0,[2] = "NumberValue"},["Tie Fighters"] = {[1] = 0,[2] = "NumberValue"},["Town Hall Old"] = {[1] = 0,[2] = "NumberValue"},["Town Hall"] = {[1] = 0,[2] = "NumberValue"},["Trailer Park"] = {[1] = 0,[2] = "NumberValue"},["Tudor"] = {[1] = 0,[2] = "NumberValue"},["Vader Statue"] = {[1] = 0,[2] = "NumberValue"},["Villa"] = {[1] = 0,[2] = "NumberValue"},["Wood Cabin"] = {[1] = 0,[2] = "NumberValue"}},["LimitedItems"] = {[2] = "table",["Empire State Building"] = {[1] = 0,[2] = "NumberValue"},["Fast Food Coliseum"] = {[1] = 0,[2] = "NumberValue"},["Fountain"] = {[1] = 0,[2] = "NumberValue"},["Golden Taco Cart"] = {[1] = 0,[2] = "NumberValue"},["Juan Statue"] = {[1] = 0,[2] = "NumberValue"},["Millennium Falcon"] = {[1] = 0,[2] = "NumberValue"},["National Bank"] = {[1] = 0,[2] = "NumberValue"},["Prison"] = {[1] = 0,[2] = "NumberValue"},["The Mystery Machine"] = {[1] = 0,[2] = "NumberValue"}},["RewardItems"] = {[2] = "table",["Beta1 Statue"] = {[1] = false,[2] = "BoolValue"},["Beta2 Statue"] = {[1] = false,[2] = "BoolValue"},["Death Star"] = {[1] = false,[2] = "BoolValue"},["Pre-Beta Statue"] = {[1] = false,[2] = "BoolValue"},["Roblox HQ"] = {[1] = false,[2] = "BoolValue"},["The Mystery Shack"] = {[1] = false,[2] = "BoolValue"},["The Sanchez Residence"] = {[1] = false,[2] = "BoolValue"},["The Walking Dead"] = {[1] = false,[2] = "BoolValue"},["Tie Fighters"] = {[1] = false,[2] = "BoolValue"},["Vader Statue"] = {[1] = false,[2] = "BoolValue"}},["Spots"] = {[2] = "table",["1"] = {[1] = "",[2] = "StringValue"},["10"] = {[1] = "",[2] = "StringValue"},["100"] = {[1] = "",[2] = "StringValue"},["101"] = {[1] = "",[2] = "StringValue"},["102"] = {[1] = "",[2] = "StringValue"},["103"] = {[1] = "",[2] = "StringValue"},["104"] = {[1] = "",[2] = "StringValue"},["105"] = {[1] = "",[2] = "StringValue"},["106"] = {[1] = "",[2] = "StringValue"},["107"] = {[1] = "",[2] = "StringValue"},["108"] = {[1] = "",[2] = "StringValue"},["109"] = {[1] = "",[2] = "StringValue"},["11"] = {[1] = "",[2] = "StringValue"},["110"] = {[1] = "",[2] = "StringValue"},["111"] = {[1] = "",[2] = "StringValue"},["112"] = {[1] = "",[2] = "StringValue"},["113"] = {[1] = "",[2] = "StringValue"},["114"] = {[1] = "",[2] = "StringValue"},["115"] = {[1] = "",[2] = "StringValue"},["116"] = {[1] = "",[2] = "StringValue"},["117"] = {[1] = "",[2] = "StringValue"},["118"] = {[1] = "",[2] = "StringValue"},["119"] = {[1] = "",[2] = "StringValue"},["12"] = {[1] = "",[2] = "StringValue"},["120"] = {[1] = "",[2] = "StringValue"},["121"] = {[1] = "",[2] = "StringValue"},["122"] = {[1] = "",[2] = "StringValue"},["123"] = {[1] = "",[2] = "StringValue"},["124"] = {[1] = "",[2] = "StringValue"},["125"] = {[1] = "",[2] = "StringValue"},["126"] = {[1] = "",[2] = "StringValue"},["127"] = {[1] = "",[2] = "StringValue"},["128"] = {[1] = "",[2] = "StringValue"},["129"] = {[1] = "",[2] = "StringValue"},["13"] = {[1] = "",[2] = "StringValue"},["130"] = {[1] = "",[2] = "StringValue"},["131"] = {[1] = "",[2] = "StringValue"},["132"] = {[1] = "",[2] = "StringValue"},["133"] = {[1] = "",[2] = "StringValue"},["134"] = {[1] = "",[2] = "StringValue"},["135"] = {[1] = "",[2] = "StringValue"},["136"] = {[1] = "",[2] = "StringValue"},["137"] = {[1] = "",[2] = "StringValue"},["138"] = {[1] = "",[2] = "StringValue"},["139"] = {[1] = "",[2] = "StringValue"},["14"] = {[1] = "",[2] = "StringValue"},["140"] = {[1] = "",[2] = "StringValue"},["141"] = {[1] = "",[2] = "StringValue"},["142"] = {[1] = "",[2] = "StringValue"},["143"] = {[1] = "",[2] = "StringValue"},["144"] = {[1] = "",[2] = "StringValue"},["145"] = {[1] = "",[2] = "StringValue"},["146"] = {[1] = "",[2] = "StringValue"},["147"] = {[1] = "",[2] = "StringValue"},["148"] = {[1] = "",[2] = "StringValue"},["149"] = {[1] = "",[2] = "StringValue"},["15"] = {[1] = "",[2] = "StringValue"},["150"] = {[1] = "",[2] = "StringValue"},["151"] = {[1] = "",[2] = "StringValue"},["152"] = {[1] = "",[2] = "StringValue"},["153"] = {[1] = "",[2] = "StringValue"},["154"] = {[1] = "",[2] = "StringValue"},["155"] = {[1] = "",[2] = "StringValue"},["156"] = {[1] = "",[2] = "StringValue"},["157"] = {[1] = "",[2] = "StringValue"},["158"] = {[1] = "",[2] = "StringValue"},["159"] = {[1] = "",[2] = "StringValue"},["16"] = {[1] = "",[2] = "StringValue"},["160"] = {[1] = "",[2] = "StringValue"},["161"] = {[1] = "",[2] = "StringValue"},["162"] = {[1] = "",[2] = "StringValue"},["163"] = {[1] = "",[2] = "StringValue"},["164"] = {[1] = "",[2] = "StringValue"},["165"] = {[1] = "",[2] = "StringValue"},["166"] = {[1] = "",[2] = "StringValue"},["167"] = {[1] = "",[2] = "StringValue"},["168"] = {[1] = "",[2] = "StringValue"},["169"] = {[1] = "",[2] = "StringValue"},["17"] = {[1] = "",[2] = "StringValue"},["18"] = {[1] = "",[2] = "StringValue"},["19"] = {[1] = "",[2] = "StringValue"},["2"] = {[1] = "",[2] = "StringValue"},["20"] = {[1] = "",[2] = "StringValue"},["21"] = {[1] = "",[2] = "StringValue"},["22"] = {[1] = "",[2] = "StringValue"},["23"] = {[1] = "",[2] = "StringValue"},["24"] = {[1] = "",[2] = "StringValue"},["25"] = {[1] = "",[2] = "StringValue"},["26"] = {[1] = "",[2] = "StringValue"},["27"] = {[1] = "",[2] = "StringValue"},["28"] = {[1] = "",[2] = "StringValue"},["29"] = {[1] = "",[2] = "StringValue"},["3"] = {[1] = "",[2] = "StringValue"},["30"] = {[1] = "",[2] = "StringValue"},["31"] = {[1] = "",[2] = "StringValue"},["32"] = {[1] = "",[2] = "StringValue"},["33"] = {[1] = "",[2] = "StringValue"},["34"] = {[1] = "",[2] = "StringValue"},["35"] = {[1] = "",[2] = "StringValue"},["36"] = {[1] = "",[2] = "StringValue"},["37"] = {[1] = "",[2] = "StringValue"},["38"] = {[1] = "",[2] = "StringValue"},["39"] = {[1] = "",[2] = "StringValue"},["4"] = {[1] = "",[2] = "StringValue"},["40"] = {[1] = "",[2] = "StringValue"},["41"] = {[1] = "",[2] = "StringValue"},["42"] = {[1] = "",[2] = "StringValue"},["43"] = {[1] = "",[2] = "StringValue"},["44"] = {[1] = "",[2] = "StringValue"},["45"] = {[1] = "",[2] = "StringValue"},["46"] = {[1] = "",[2] = "StringValue"},["47"] = {[1] = "",[2] = "StringValue"},["48"] = {[1] = "",[2] = "StringValue"},["49"] = {[1] = "",[2] = "StringValue"},["5"] = {[1] = "",[2] = "StringValue"},["50"] = {[1] = "",[2] = "StringValue"},["51"] = {[1] = "",[2] = "StringValue"},["52"] = {[1] = "",[2] = "StringValue"},["53"] = {[1] = "",[2] = "StringValue"},["54"] = {[1] = "",[2] = "StringValue"},["55"] = {[1] = "",[2] = "StringValue"},["56"] = {[1] = "",[2] = "StringValue"},["57"] = {[1] = "",[2] = "StringValue"},["58"] = {[1] = "",[2] = "StringValue"},["59"] = {[1] = "",[2] = "StringValue"},["6"] = {[1] = "",[2] = "StringValue"},["60"] = {[1] = "",[2] = "StringValue"},["61"] = {[1] = "",[2] = "StringValue"},["62"] = {[1] = "",[2] = "StringValue"},["63"] = {[1] = "",[2] = "StringValue"},["64"] = {[1] = "",[2] = "StringValue"},["65"] = {[1] = "",[2] = "StringValue"},["66"] = {[1] = "",[2] = "StringValue"},["67"] = {[1] = "",[2] = "StringValue"},["68"] = {[1] = "",[2] = "StringValue"},["69"] = {[1] = "",[2] = "StringValue"},["7"] = {[1] = "",[2] = "StringValue"},["70"] = {[1] = "",[2] = "StringValue"},["71"] = {[1] = "",[2] = "StringValue"},["72"] = {[1] = "",[2] = "StringValue"},["73"] = {[1] = "",[2] = "StringValue"},["74"] = {[1] = "",[2] = "StringValue"},["75"] = {[1] = "",[2] = "StringValue"},["76"] = {[1] = "",[2] = "StringValue"},["77"] = {[1] = "",[2] = "StringValue"},["78"] = {[1] = "",[2] = "StringValue"},["79"] = {[1] = "",[2] = "StringValue"},["8"] = {[1] = "",[2] = "StringValue"},["80"] = {[1] = "",[2] = "StringValue"},["81"] = {[1] = "",[2] = "StringValue"},["82"] = {[1] = "",[2] = "StringValue"},["83"] = {[1] = "",[2] = "StringValue"},["84"] = {[1] = "",[2] = "StringValue"},["85"] = {[1] = "",[2] = "StringValue"},["86"] = {[1] = "",[2] = "StringValue"},["87"] = {[1] = "",[2] = "StringValue"},["88"] = {[1] = "",[2] = "StringValue"},["89"] = {[1] = "",[2] = "StringValue"},["9"] = {[1] = "",[2] = "StringValue"},["90"] = {[1] = "",[2] = "StringValue"},["91"] = {[1] = "",[2] = "StringValue"},["92"] = {[1] = "",[2] = "StringValue"},["93"] = {[1] = "",[2] = "StringValue"},["94"] = {[1] = "",[2] = "StringValue"},["95"] = {[1] = "",[2] = "StringValue"},["96"] = {[1] = "",[2] = "StringValue"},["97"] = {[1] = "",[2] = "StringValue"},["98"] = {[1] = "",[2] = "StringValue"},["99"] = {[1] = "",[2] = "StringValue"}},["TimerValue"] = {[1] = 0,[2] = "NumberValue"},["Tutorial"] = {[1] = 1,[2] = "NumberValue"},["TycoonStats"] = {[2] = "table",["BigBuildings"] = {[1] = 0,[2] = "NumberValue"},["Income"] = {[1] = 0,[2] = "NumberValue"},["MaxInventory"] = {[1] = 10,[2] = "NumberValue"},["Money"] = {[1] = 0,[2] = "NumberValue"},["NameBox"] = {[1] = player.Name.."'s City",[2] = "StringValue"},["ReqCitizens"] = {[1] = 0,[2] = "NumberValue"},["Roads"] = {[1] = 0,[2] = "NumberValue"},["Speed"] = {[1] = 17,[2] = "NumberValue"},["TotalCitizens"] = {[1] = 0,[2] = "NumberValue"}}}
local leaderstats = Instance.new("Configuration")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Value = 0
Cash.Name = "Cash"
Cash.Parent = leaderstats
local Relocation = Instance.new("NumberValue")
Relocation.Value = 0
Relocation.Name = "Relocation"
Relocation.Parent = leaderstats
local Success, Error
Success, Error = pcall(function()
pdData = database:GetAsync(player.UserId)
end)
if Success then
if pdData then
pdData = http:JSONDecode(pdData)
print(pdData)
DeserializeData(pdData, "PlayerData", player) -- For all PlayerData
else
DeserializeData(defaultTable, "PlayerData", player) -- For all PlayerData
end
else
print(Error)
end
local Success, Error
Success, Error = pcall(function()
Data = database:GetAsync("Relocation-"..player.UserId)
Data2 = database:GetAsync("Cash-"..player.UserId)
end)
if Success then
if Data then
Relocation.Value = Data
end
if Data2 then
Cash.Value = Data2
end
else
print(Error)
end
end
local function playerRemoving(player)
local success, errormsg
success, errormsg = pcall(function()
database:SetAsync("Cash-"..player.UserId, player.leaderstats.Cash.Value)
database:SetAsync("Relocation-"..player.UserId, player.leaderstats.Relocation.Value)
end)
if success then
print("Saved "..player.Name.."'s Data")
else
print(errormsg)
end
local pdata = SerializeData(player:WaitForChild("PlayerData"))
pdata = http:JSONEncode(pdata)
-- pcalls :yawn:
local attempts = 5
local function attemptSave()
attempts -= 1
local success, errormsg
success, errormsg = pcall(function()
database:SetAsync(player.UserId, pdata)
print(pdata)
end)
if not success then
print(errormsg)
print("Retrying "..tostring(attempts))
wait(3)
if attempts > 0 then
attemptSave()
else
print("Data was unable to save successfully. rip")
end
end
end
attemptSave()
end
players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerRemoving)
Here is a picture of the error I am getting:
If anyone knows how I can fix this, please do as we have been racking our brains for so long.
Thanks,
– Ham