I see a lot of people having the same issues with this issue, including myself sometimes.
local datastore = game:GetService("DataStoreService"):GetDataStore("Data")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local Gold = Instance.new("NumberValue",leaderstats)
Gold.Name = "Gold"
local Rebirths = Instance.new("NumberValue",leaderstats)
Rebirths.Name = "Rebirths"
local MegaCoins = Instance.new("NumberValue",leaderstats)
MegaCoins.Name = "MegaCoins"
MegaCoins.Value = 0
local debounce = Instance.new("BoolValue")
debounce.Value = false
debounce.Name = "Debounce"
debounce.Parent = player
local key = plr.UserId -- to keep it simple
local storeditems = datastore:GetAsync(key)
if storeditems ~= nil then -- anything, BUT nil.
print("Found data for " .. plr.Name)
Gold.Value = storeditems[1]
Rebirths.Value = storeditems[2]
MegaCoins.Value = storeditems[3]
else
print("Could not find data for " .. plr.Name .. " replacing no data with data")
Gold.Value = 0
Rebirths.Value = 0
MegaCoins.Value = 0
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local datasave = {}
table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("Gold").Value)
table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("Rebirths").Value)
table.insert(datasave, plr:WaitForChild("leaderstats":WaitForChild("MegaCoins").Value)
local success, resp = pcall(function()
datastore:SetAsync(plr.UserId, datasave) -- key & then the data
end)
if success then
print("Succesfully saved data of " .. plr.Name)
else
warn(resp) -- basically if there is an error it will say what it is.
end
end)