Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does 'DataStore':GetAsync(player.userId) return nil?

Asked by 8 years ago

So, I'm playing around with data stores and attempting to change a playerAs data store values based on another playerBs actions.. Sadly, I apparently can't get playerAs data to update.. it always returns nil..

function DataStore:UpdateStatus(player, isOnline)
    output("Update")
    if not DataStore.isPlayerLoaded(player) then
        repeat wait() output("Waiting for Player:", player)
        until DataStore.isPlayerLoaded(player)
    else
        output("Player is loaded.", player)
        output("DataSize:", #sessionData[player])
    end

    for i = 1, #sessionData[player] do
        local friendID = game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"])
        output("DEBUG", friendID)
        if friendID ~= nil then
            output(player, ":", sessionData[player][i]["Name"], "::", friendID)
        end

        local playerData = FriendData:GetAsync(friendID)

        output("DEBUG", playerData)

        if playerData ~= nil then           
            for i, v in pairs(playerData) do
                output("DEBUG", i, v)
            end

            FriendData:SetAsync(game.Players:GetUserIdFromNameAsync(sessionData[player][i]["Name"], playerData))
        end
    end
end

Output

[MOD_SERVER_FRIENDS] DEBUG 26517
[MOD_SERVER_FRIENDS] Player1 : Player2 :: 26517
[MOD_SERVER_FRIENDS] DEBUG nil
0
It always returns nil because it isn't set to anything. A data store will return nil if the key has not been used or there's simply nothing in it. Uglypoe 557 — 8y

1 answer

Log in to vote
0
Answered by
Vexture 179
8 years ago

I believe it is returning nil because there is no existing key/save for that player. Add a conditional (if) statement that checks if a save does not exist. If the save doesn't exist, then create one for the player.

Ad

Answer this question