Hi, right now i having a problem with my datastore but I cant find what’s wrong with it. some players lose their data for no reason in my tower defense game.
I made the datastore saving/load in a module script
Data Load:
function datastore:Load(player)
if game.Players:FindFirstChild(player.Name) == nil then warn("Player not found; "..player.Name) return end
local playerfolder = playerdatafolder:FindFirstChild(player.Name.." Folder")
local datatable = {}
local dataerrortable = {}
local codered = false
local success, fail = pcall(function()
datatable = dsplayer:GetAsync("player-"..player.UserId)
end)
wait()
if success then
warn("Successfully load player data, Name: "..player.Name.." UserId: "..player.UserId)
else
warn("Failed to load player data, Name: "..player.Name.." UserId: "..player.UserId.." Errors; "..fail)
replicatedstorage.GameFolder.EventFolder.PlayerEvent.LocalPrint:FireClient(player, "Your data failed to load, please report this to developers. Your data wont save after leaving the game. Error: "..fail, "Warn")
end
spawn(function()
tableprintmodule.Print(datatable, player.UserId.." Data", true)
end)
local success, fail = pcall(function()
for i, currency in pairs(playerfolder.PlayerCurrency:GetChildren()) do
if currency then
if datatable["Currency"] then
if datatable["Currency"][currency.Name] then
currency.Value = datatable["Currency"][currency.Name]
end
end
end
end
end)
if success then
print(player.Name, "Currency Code: Green")
else
warn(player.Name, "Currency Code: Red")
dataerrortable = player.Name.." Currency Data failed to load; "..fail
end
local success, fail = pcall(function()
for i, towerslot in pairs(playerfolder.PlayerTowerSlot:GetChildren()) do
if towerslot then
if datatable["TowerSlot"] then
if datatable["TowerSlot"]["Slot"..i] then
towerslot.Value = datatable["TowerSlot"]["Slot"..i]
end
end
end
end
end)
if success then
print(player.Name, "TowerSlot Code: Green")
else
warn(player.Name, "TowerSlot Code: Red")
dataerrortable = player.Name.." TowerSlot Data failed to load; "..fail
end
local success, fail = pcall(function()
for i, tower in pairs(playerfolder.PlayerTower:GetChildren()) do
if tower then
if datatable["Tower"] then
if datatable["Tower"][tower.Name] then
tower.Purchased.Value = datatable["Tower"][tower.Name].Purchased
end
end
end
end
end)
if success then
print(player.Name, "Tower Inv Code: Green")
else
warn(player.Name, "Tower Inv Code: Red")
dataerrortable = player.Name.." Tower Inventory Data failed to load; "..fail
end
local success, fail = pcall(function()
for i, otherdata in pairs(playerfolder.PlayerOtherData:GetChildren()) do
if otherdata then
if datatable["Other"] then
if datatable["Other"][otherdata.Name] then
otherdata.Value = datatable["Other"][otherdata.Name]
end
end
end
end
end)
if success then
print(player.Name, "Other Data Code: Green")
else
warn(player.Name, "Other Data Code: Red")
dataerrortable = player.Name.." Other Data failed to load; "..fail
end
replicatedstorage.GameFolder.EventFolder.PlayerEvent.PlayerDataLoaded:FireClient(player, player.Name)
if codered == false then
player.Dataloaded.Value = true
replicatedstorage.GameFolder.EventFolder.PlayerEvent.LocalPrint:FireClient(player, "Your data successfully loaded. ", "Print")
else
player.Dataloaded.Value = false
--replicatedstorage.GameFolder.EventFolder.PlayerEvent.LocalPrint:FireClient(player, "Your data failed to load, please report this to developers. Your data wont save after leaving the game.", "Warn")
for i, logtext in ipairs(dataerrortable) do
if logtext then
replicatedstorage.GameFolder.EventFolder.PlayerEvent.LocalPrint:FireClient(player, logtext, "Warn")
end
end
end
end
Data Saving;
function datastore:Save(player)
if player.Dataloaded.Value == false then return end
local playerfolder = playerdatafolder:FindFirstChild(player.Name.." Folder")
local datatable = {
["Tower"] = {};
["Currency"] = {};
["TowerSlot"] = {
["Slot1"] = nil;
["Slot2"] = nil;
["Slot3"] = nil;
["Slot4"] = nil;
["Slot5"] = nil;
};
["Other"] = {
["MatchID"] = "";
};
}
spawn(function()
local success, failure = pcall(function()
for i, currency in pairs(playerfolder.PlayerCurrency:GetChildren()) do
if currency then
datatable.Currency[currency.Name] = currency.Value
end
end
for i, towerdata in pairs(playerfolder.PlayerTower:GetChildren()) do
if towerdata then
datatable.Tower[towerdata.Name] = {
["Purchased"] = towerdata.Purchased.Value
}
end
end
for i, towerslot in pairs(playerfolder.PlayerTowerSlot:GetChildren()) do
if towerslot then
datatable.TowerSlot[towerslot.Name] = towerslot.Value
end
end
for i, otherdata in pairs(playerfolder.PlayerOtherData:GetChildren()) do
if otherdata then
if datatable.Other[otherdata.Name] then
datatable.Other[otherdata.Name] = otherdata.Value
end
end
end
end)
spawn(function()
tableprintmodule.Print(datatable, player.UserId.." Data", true)
end)
if success then
print("succcessfully load; ", player.Name, "data into table")
end
local success2, fail = pcall(function()
dsplayer:SetAsync("player-"..player.UserId, datatable)
end)
if success2 then
print("successfully save; ", player.Name)
end
wait(1)
playerfolder:Destroy()
end)
end
Any ideas on how to resolve it?, I’ve been trying to fix it for a few days since my game opened to the public. When my testers tested the game this issue never happen.