Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RunService = game:GetService("RunService")
- local DataStoreService = game:GetService("DataStoreService")
- local dataStore = DataStoreService:GetDataStore("MyDataStore")
- local function saveData(player) -- The functions that saves data
- local tableToSave = {
- player.leaderstats.Wins.Value;
- player.leaderstats.Kills.Value;
- player.leaderstats.Deaths.Value
- }
- local success, err = pcall(function()
- dataStore:SetAsync(player.UserId, tableToSave)
- end)
- if success then
- print("Data has been saved!")
- else -- Else if the save failed
- print("Data hasn't been saved!")
- warn(err)
- end
- end
- game.Players.PlayerAdded:Connect(function(player)
- -- // Assigning player stats //
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local Wins = Instance.new("IntValue")
- Wins.Name = "Wins"
- Wins.Parent = leaderstats
- local Kills = Instance.new("IntValue")
- Kills.Name = "Kills"
- Kills.Parent = leaderstats
- local Deaths = Instance.new("IntValue")
- Deaths.Name = "Deaths"
- Deaths.Parent = leaderstats
- local data
- local success, err = pcall(function()
- data = dataStore:GetAsync(player.UserId)
- end)
- if success and data then
- Wins.Value = data[1]
- Kills.Value = data[2]
- Deaths.Value = data[3]
- else
- print("The player has no data!")
- end
- end)
- game.Players.PlayerRemoving:Connect(saveData)
- game:BindToClose(function()
- if RunService:IsStudio() then
- task.wait(2)
- else
- for _, player in ipairs(game.Players:GetPlayers()) do
- coroutine.wrap(saveData)(player)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement