Data not loading correctly when a player joins the game

Hi there!

So I’ve got this data saving system, which converts folders, strings, numbers, etc into tables to save them into a data store. The saving is completely fine, but when a player joins the game the data just entirely breaks.

Below I’ve shown what happens before you rejoin and after, as well as the scripts that load the data on rejoining as I assume it’s the one causing issues.

Note: Profile Service is used for data saving

Before Leaving:

Original Data:
image

Original Converted Data:
image

After Rejoining

Messed up data:
image

Converted Data:
image

Scripts used to load data:

local function createData(data, parent)
    for dataLabel, dataValue in pairs(data) do
        
        warn(dataLabel, dataValue)


        local type = dataTypeToInstanceType(typeof(dataValue))
        if type == "Folder" then
            local DataInstance = createInstance(type, {Name = dataLabel})
            createData(dataValue, DataInstance)
            DataInstance.Parent = parent
        else
            local DataInstance = createInstance(type, {Name = dataLabel, Value = dataValue})
            DataInstance.Parent = parent
        end
    end
end

function Data.CreatePlayerDataInstances(Player, Profile : {}?)
    Profile = Profile or Data.LoadedProfiles[Data.CreateProfileKey(Player)]
    Profile.Data.CashOutRate = tonumber(Profile.Data.CashOutRate)
    print(Profile.Data.Games)
    
    local DataFolder = Instance.new("Folder")
    DataFolder.Name = "Stats"

    local LeaderStats = createInstance("Folder", {Name = "leaderstats", Parent = Player})
    local FakeCash = createInstance("StringValue", {Value = Profile.Data.Cash, Parent = LeaderStats, Name = "Cash"})
    local FakeVisits = createInstance("StringValue", {Value = Profile.Data.AllTimeVisits, Parent = LeaderStats, Name = "Visits"})
    
    print("PROFILE ON ENTRY: ",Profile.Data)
    createData(Profile.Data, DataFolder)
   

    DataFolder.Parent = Player
    
    local Playtime = DataFolder.Playtime
    task.spawn(function()
        while game.Players:FindFirstChild(Player.Name) do
            Playtime.Value += 1
            task.wait(1)
        end
    end)    
end

I have no clue why this is happening, and I’ve tried to implement things to try and fix it, but they all end up with the same result.

If anyone has any ideas on why this is happening and how to fix it, please let me know. Thanks!

I am pretty sure that type is a pre-assigned value try changing it to dataType

I doubt that’s the issue as it stores other data as well, and it seems to load those correctly, just not this portion of it.

What is the output from this line of code

Edit: try printing the type it may not be recognized as “Folder”

I thought it would be that, but I tried removing folders from the data completely, but still didn’t change the output.

It would output someone like: Visits,50

If you print out the length of data from

using the # operator
What number does it give you?

That function doesn’t return anything, it just creates like NumberValues, StringValues, etc.

I know, since it creates the values and folders it would be checking if the Datastore was able to store the folders in the first place

Well, the folders are converted to tables in the data store. But either way, I completely removed folders from the equation and the output did not change.

What values does it load correctly?

Everything that isn’t that games folder, loads correctly.

If you removed the folders why does the output stay the same?
image
Is this the output after folders were removed

Yep, don’t know why but it doesn’t change.

Maybe the data isn’t saving because of the issue. try setting the save data to an empty table

1 Like

The data 100% saves. When I check the data store after leaving the game, I can see it update.

when you print the type what values do you get back?

The data might save, but are you sure you’re saving it correctly?