``local dataStores = game:GetService("DataStoreService"):GetDataStore("Data1")
local defaultCash = 10
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1 local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local bucks = Instance.new("IntValue") bucks.Name = "bucks" bucks.Parent = leaderstats bucks.Value = 0
local player_data
pcall(function() player_data = dataStores:GetAsync(player.UserId.."-Bucks") end)
if player_data ~= nil then bucks.Value = player_data else bucks.Value = defaultCash
end
end)
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function() dataStores:SetAsync(player.UserId.."-bucks",player.leaderstats.bucks.Value) print("Saved") playersLeft = playersLeft - 1 bindableEvent:Fire() end)
end)
game:BindToClose(function()
while playersLeft > 0 do bindableEvent.Event:Wait() end
end)