script to save - local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CoinsStore = DataStoreService:GetDataStore("CoinsData")
Players.PlayerAdded:Connect(function(player)
-- Create leaderstats folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create Coins stat
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
-- Load saved data
local success, data = pcall(function()
return CoinsStore:GetAsync(player.UserId)
end)
if success and data then
coins.Value = data
else
coins.Value = 100 -- default value
end
end)
-- Save data when player leaves
Players.PlayerRemoving:Connect(function(player)
local coins = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Coins")
if coins then
pcall(function()
CoinsStore:SetAsync(player.UserId, coins.Value)
end)
end
end)