Skip to content

Instantly share code, notes, and snippets.

@MajorTal
Created May 30, 2025 18:27
Show Gist options
  • Select an option

  • Save MajorTal/abefb563f5a8137777318686567349ad to your computer and use it in GitHub Desktop.

Select an option

Save MajorTal/abefb563f5a8137777318686567349ad to your computer and use it in GitHub Desktop.
local DataStoreService = game:GetService("DataStoreService")
local cashDataStore = DataStoreService:GetDataStore("PlayerCash")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
-- Load player's saved cash or default to 100
local success, savedCash = pcall(function()
return cashDataStore:GetAsync(player.UserId)
end)
if success and savedCash then
cash.Value = savedCash
else
cash.Value = 100 -- default initial value
end
end)
Players.PlayerRemoving:Connect(function(player)
local cash = player.leaderstats.Cash.Value
-- Save player's cash
pcall(function()
cashDataStore:SetAsync(player.UserId, cash)
end)
end)
-- Save all data when game closes or server shuts down
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
local cash = player.leaderstats.Cash.Value
pcall(function()
cashDataStore:SetAsync(player.UserId, cash)
end)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment