Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --LeaderBoard Script:
- game.Players.PlayerAdded:connect(function(p)
- local stats = Instance.new("IntValue")
- stats.Name = "leaderstats"
- stats.Parent = p
- local money = Instance.new("IntValue")
- money.Name = "Money"
- money.Value = 10
- money.Parent = stats
- end)
- --Datastore Script:
- local AUTO_SAVE = false -- Make true to enable auto saving
- local TIME_BETWEEN_SAVES = 60 -- In seconds (WARNING): Do not put this lower than 60 seconds
- local PRINT_OUTPUT = false -- Will print saves and loads in the output
- local SAFE_SAVE = false -- Upon server shutdown, holds server open to save all data
- ---------------------------------
- local players = game:GetService("Players")
- local dataStoreService = game:GetService("DataStoreService")
- local leaderboardData = dataStoreService:GetDataStore("LeaderStats")
- local function Print(message)
- if PRINT_OUTPUT then print(message) end
- end
- local function SaveData(player)
- if player.userId < 0 then return end
- player:WaitForChild("leaderstats")
- wait()
- local leaderstats = {}
- for i, stat in pairs(player.leaderstats:GetChildren()) do
- table.insert(leaderstats, {stat.Name, stat.Value})
- end
- leaderboardData:SetAsync(player.userId, leaderstats)
- Print("Saved "..player.Name.."'s data")
- end
- local function LoadData(player)
- if player.userId < 0 then return end
- player:WaitForChild("leaderstats")
- wait()
- local leaderboardStats = leaderboardData:GetAsync(player.userId)
- for i, stat in pairs(leaderboardStats) do
- local currentStat = player.leaderstats:FindFirstChild(stat[1])
- if not currentStat then return end
- currentStat.Value = stat[2]
- end
- Print("Loaded "..player.Name.."'s data")
- end
- players.PlayerAdded:connect(LoadData)
- players.PlayerRemoving:connect(SaveData)
- if SAFE_SAVE then
- game.OnClose = function()
- for i, player in pairs(players:GetChildren()) do
- SaveData(player)
- end
- wait(1)
- end
- end
- while AUTO_SAVE do
- wait(TIME_BETWEEN_SAVES)
- for i, player in pairs(players:GetChildren()) do
- SaveData(player)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement