Script says I have no data, even though data was saved

Hello, so I am making a datastore script to save player data on a JoJo game I am working on, I’m using a module script and have API turned on.

So I’ve made a working datastore script, however it always says that I have no data, even though I’ve saved my data, why is this happening? No errors aswell.

Here’s my code, sory if it’s a bit messy, I tried my best to clean it up.

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("DataStore1")

local DataModule =  {}
local sessionData = {}
local noSave = {}

function DataModule.LoadData(player, retries)
    local playerId = player.UserId
    local currentRetries = 0

    local success, errormsg = pcall(function()
        myData = myDataStore:GetAsync(playerId)
    end)

    if success then

       -- alright so here is the problem, here it checks if I had data saved, but it always returns no, even though I cleary saved new data
        if myData then
            sessionData[playerId] = myData
            return true
        else
            -- it always returns this, even though I saved my data
            sessionData[playerId] = {Stand = "Standless"}
            return true
        end
    else
        noSave[playerId] = true
    end
    
    return true
end

function DataModule.SaveData(player, retries)
    local playerId = player.UserId
    local currentRetries = 0
    
    if noSave[playerId] then
        return
    end
    
    if sessionData[playerId] then
        local success, errormsg = pcall(function()
            myDataStore:SetAsync(sessionData[playerId], playerId)
        end)
        
        if success then
            return
        else
            repeat
                local success, errormsg = pcall(function()
                    myDataStore:SetAsync(sessionData[playerId], playerId)
                end)
                
                currentRetries += 1
            until success or currentRetries >= retries
            
            if success then
                return
            else
                return
            end
        end
    end
end

function DataModule.SetData(player, dataName, newData)
    local playerId = player.UserId

    if sessionData[playerId] then
        sessionData[playerId][dataName] = newData
    end
end

game.Players.PlayerAdded:Connect(function(player)
    -- loads the data, then I change it to something else so I can save it later (for testing)
    DataModule.LoadData(player, 3)
    DataModule.SetData(player, "Stand", "TheWorld")
end)

game.Players.PlayerRemoving:Connect(function(player)
    -- saves the data that I changed above, but when loading it says I had no data even though I cleary set it in the code above
    DataModule.SaveData(player, 3)
end)

return DataModule

I tried doing some solutions, but none of them worked. I saw someone saying that you can’t save a table to a datastore, but I’m pretty sure I’ve done it before, so I don’t think it’s that, and even if it is, no idea how I would save my data as a string and then load it later.

I do not find any issue with your code, you can maybe try debugging your code more, and if it doesn’t work, might use a different Data Store module, for Instance DataStore2, I use it for my Simulator game to save tables, and it works just perfectly fine.

1 Like

I don’t know if this is the problem but you never made a variable called “MyData”

--It should be (I think?) 
    local myData
    local success, errormsg = pcall(function()
        myData = myDataStore:GetAsync(playerId)
    end)

I’ve never used a module for data so excuse me if I’m wrong.

1 Like

I didn’t make it a local variable, it’s a global variable so that I could use it outside the pcall, but I’ve also tried making a local variable before the pcall and then setting it to a value (like you did) but it did not change anything

1 Like

Hello, I’ve found the solution, so if you have a similar problem to mine I recommend checking this:

local success, errormsg = pcall(function()
	-- this is the problem, the playerId (datastore key) and the actual data I want to save are flipped
	myDataStore:SetAsync(sessionData[playerId], playerId)

	-- so instead of that I should've done this:
	myDataStore:SetAsync(playerId, sessionData[playerId])
end)

It’s a really simple fix, although a bit hard to spot. I hope this helped you if you have a similar problem!