How can you access datastore even if a player is not in game?

How can I “see” how much coins someone has without them being on?



game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local Coins = Instance.new("NumberValue")
	Coins.Parent = leaderstats
        Coins.Name = "Coins"
	leaderstats.Parent = player

	local save = dataStore:GetAsync(player.UserId.."_save")
	if save ~= nil then
		player.leaderstats.Coins.Value = save
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local saving = player.leaderstats.Coins.Value
	dataStore:SetAsync(player.UserId.."_save", saving)
end)


local UserId = game.Players:GetUserIdFromNameAsync("raid6n")
local key = (UserId.."_save")
print(dataStore:GetAsync(key))  -- prints nothing
1 Like

Are you sure there is something saved on that key? There seems to be no apparent issues with the code you provided.

1 Like

I have 0 Coins, is that the problem?

No, that should still show up. If it prints nil then that means there is no data on the key whatsoever. This most likely means the data was never saved, make sure “studio access to api services” is enabled.

1 Like

I tested it in studio, that’s probably why it didn’t print anything.

That seems reasonable. You could try and use BindToClose to save the data of all existing players once the server closes. This should also make sure that the server does not shutdown until the data has been saved.

1 Like