I am currently trying to create a game, where if you win a round you earn 'Gems', but when you leave the game, they save for the next time you join the game. This is not what happens though, if you leave and rejoin, the gems are not saved, and I am unsure why, there is no error in the console when I test
Edit: I already have API Services turned on, and it still doesn't work.
local DSService = game:GetService('DataStoreService'):GetDataStore('Gems')
game.Players.PlayerAdded:Connect(function(plr)
-- Defining variables
local uniquekey = 'id-'..plr.userId
local leaderstats = Instance.new('IntValue', plr)
local savevalue = Instance.new('IntValue')
leaderstats.Name = 'leaderstats'
savevalue.Parent = leaderstats
savevalue.Name = 'Gems'
-- GetAsync
local GetSaved = DSService:GetAsync(uniquekey)
if GetSaved then
savevalue.Value = GetSaved[1]
else
local NumbersForSaving = {savevalue.Value}
DSService:SetAsync(uniquekey, NumbersForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local uniquekey = 'id-'..plr.userId
local Savetable = {plr.gems.Gems.Value}
DSService:SetAsync(uniquekey, Savetable)
end)

6 comments
Try using game:BindToClose.
If you are doing this is a solo studio test, the game shuts before PlayerRemoving fires. REMEMBER TO ALWAYS USE PCALL() ON DATASTORE OPERATIONS
I'm confused ^^
DId you test in a server, or in studio? For testing data-saving related to when a player leaves, you'd want to use a proper server.
Try turning on API services in your dev page
Already did it.