--var local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("MoneyData") local ds2 = datastore:GetDataStore("LevelData") local ds3 = datastore:GetDataStore("RebirthData")
local cooldown = 60
--work game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = plr local money = Instance.new("IntValue") money.Name = "Money" money.Parent = leaderstats local level = Instance.new("IntValue") level.Name = "Level" level.Parent = leaderstats local rebirths = Instance.new("IntValue") rebirths.Name = "Rebirths" rebirths.Parent = leaderstats local moneyData -- money local successMoney, errorMoney = pcall(function() moneyData = ds1:GetAsync(plr.UserId.." _money") end) local levelData -- level local successLevel, errorLevel = pcall(function() levelData = ds2:GetAsync(plr.UserId.." _level") end) local rebData -- rebirths local successReb, errorReb = pcall(function() rebData = ds3:GetAsync(plr.UserId.." _rebirths") end) if successMoney then money.Value = moneyData else print("We couldnt store the money") warn(errorMoney) end if successLevel then level.Value = levelData else print("We couldnt store the level") warn(errorLevel) end if successReb then rebirths.Value = rebData else print("We couldnt store the reborths") warn(errorReb) end while wait(cooldown) do ds1:SetAsync(plr.UserId.." _money", money.Value) ds2:SetAsync(plr.UserId.." _level", level.Value) ds3:SetAsync(plr.UserId.." _rebirths", rebirths.Value) print("Data autosaved") end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local successMoney, errorMoney = pcall(function() ds1:SetAsync(plr.UserId.." _money", plr.leaderstats.Money.Value) end) local successLevel, errorLevel = pcall(function() ds2:SetAsync(plr.UserId.." _level", plr.leaderstats.Level.Value) end) local successReb, errorReb = pcall(function() ds3:SetAsync(plr.UserId.." _rebirths", plr.leaderstats.Rebirths.Value) end) if successMoney then print("money data stored successfully") else print("We couldnt store the money") warn(errorMoney) end if successLevel then print("level data stored successfully") else print("We couldnt store the level") warn(errorLevel) end if successReb then print("rebirths data stored successfully") else print("We couldnt store the rebirths") warn(errorReb) end
end)
Saving by module: https://developer.roblox.com/en-us/articles/Saving-Player-Data
Saving leaderstats: https://devforum.roblox.com/t/help-with-datastores/72001 (this should help you)