local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.userId
local save1 = plr.leaderstats.Level
local save2 = plr.leaderstats.Mists
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
else
local NumberForSaving = {save1.Value, save2.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.userId, {plr.leaderstats.Level.Value, plr.leaderstats.Mists.Value})
end)
✓ ANSWER ACCEPTED
My data store still wont work. I keep rejoining but it wont save. Why?
1 answer
✓ Accepted answer
If you haven't enable your API Services then enable it
Also keep in mind, roblox studio server shutdowns as the client leaves. Giving no time for the server to react to your .PlayerRemoving event
There's an easy fix by using game:BindToClose(). You must have this in your game if you are using datastore 1 because when your online server shutdowns, there's a big chance a data loss will occur
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose
local function save()
-- your saving code here
end
game.Players.PlayerRemoving:Connect(function(plr)
if not game:GetService("RunService"):IsStudio() then
save(plr) -- ONLY RUNS IF ITS AN ONLINE SERVER
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
save(player)
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
I am also having the same problen I even typed the scrip almost identical to yours
Did you enable API Services?