Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

1 comment

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.

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.