local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local moneyStore = DataStoreService:GetDataStore("PlayerMoney")
local function setupLeaderstats(player, cashValue)
-- Remove old leaderstats if they exist
local oldStats = player:FindFirstChild("leaderstats")
if oldStats then
oldStats:Destroy()
end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Cash"
money.Value = cashValue or 0
money.Parent = leaderstats
local seatvalue = Instance.new("BoolValue")
seatvalue.Name = "SeatValue"
seatvalue.Value = false
seatvalue.Parent = leaderstats
-- Save money when it changes
money.Changed:Connect(function()
-- Use money.Value instead of the event argument (which is a string)
pcall(function()
moneyStore:SetAsync(player.UserId, money.Value)
end)
end)
end
local function onPlayerAdded(player)
-- Load saved money
local cash = 1000 -- default starting cash
local success, result = pcall(function()
return moneyStore:GetAsync(player.UserId)
end)
if success and result ~= nil then
cash = result
end
setupLeaderstats(player, cash)
end
local function onPlayerRemoving(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local money = leaderstats:FindFirstChild("Cash")
if money then
pcall(function()
moneyStore:SetAsync(player.UserId, money.Value)
end)
end
end
end
** You are now Level 2! **