local DataStoreService = game:GetService("DataStoreService")
local playerGold = DataStoreService:GetDataStore("playerGold")
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue", leaderstats)
gold.Name = "Gold"
local success, data = pcall(function()
return playerGold:GetAsync("Player_"..player.UserId)
end)
if success then
gold.Value = data
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
local function onPlayerLeave(player)
local success, err = pcall(function()
playerGold:SetAsync("Player_"..player.UserId, player.leaderstats.Gold.Value)
end)
end
game.Players.PlayerRemoving:Connect(onPlayerLeave)