local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("PlayerData")game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
end)game.Players.PlayerAdded:Connect(function(player)
local data
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
player.leaderstats.Coins.Value = data
end
end)game.Players.PlayerRemoving:Connect(function(player)
local coins = player.leaderstats.Coins.Value
pcall(function()
dataStore:SetAsync(player.UserId, coins)
end)
end)