Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local playerStatsDataStore = DataStoreService:GetDataStore("PlayerStats")
- game.Players.PlayerAdded:Connect(function(player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local wins = Instance.new("IntValue")
- wins.Name = "Wins"
- wins.Value = 0
- wins.Parent = leaderstats
- local cash = Instance.new("IntValue")
- cash.Name = "Cash"
- cash.Value = 0
- cash.Parent = player
- local success, data = pcall(function()
- return playerStatsDataStore(player.UserId)
- end)
- if success then
- if data then
- wins.Value = data.Wins or 0
- cash.Value = data.Cash or 0
- end
- else
- warn("Data loading failed for "..player.Name)
- end
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- local leaderstats = player:FindFirstChild("leaderstats")
- if leaderstats then
- local wins = leaderstats:FindFirstChild("Wins")
- local cash = leaderstats:FindFirstChild("Cash")
- if wins and cash then
- local playerData = {
- Wins = wins.Value,
- Cash = cash.Value
- }
- local success, err = pcall(function()
- playerStatsDataStore:SetAsync(player.UserId, playerData)
- end)
- if not success then
- warn("Data saving failed for "..player.Name..": ".. err)
- end
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement