script dont work(its create leader stats and save it on one script)
-- Services
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
-- Function to save player data
local function savePlayerData(player)
local success, err = pcall(function()
local playerKey = "Player_" .. player.UserId
local stats = player:FindFirstChild("leaderstats")
if stats then
local clicks = stats:FindFirstChild("Clicks")
local gems = stats:FindFirstChild("Gems")
local rebirths = stats:FindFirstChild("Rebirths")
playerDataStore:SetAsync(playerKey, {
Clicks = clicks and clicks.Value or 0,
Gems = gems and gems.Value or 0,
Rebirths = rebirths and rebirths.Value or 0
})
end
end)
if success then
print("Data saved for player: " .. player.Name)
else
warn("Failed to save data for " .. player.Name .. ": " .. tostring(err))
end
end
-- Function to load player data
local function loadPlayerData(player)
local success, err = pcall(function()
local playerKey = "Player_" .. player.UserId
local data = playerDataStore:GetAsync(playerKey)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
-- Create "Clicks" stat
local clicks = Instance.new("IntValue")
clicks.Name = "Clicks"
clicks.Value = data and data.Clicks or 0
clicks.Parent = stats
-- Create "Gems" stat
local gems = Instance.new("IntValue")
gems.Name = "Gems"
gems.Value = data and data.Gems or 0
gems.Parent = stats
-- Create "Rebirths" stat
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Value = data and data.Rebirths or 0
rebirths.Parent = stats
end)
if success then
print("Data loaded for player: " .. player.Name)
else
warn("Failed to load data for " .. player.Name .. ": " .. tostring(err))
end
end
-- Player Added Event
game.Players.PlayerAdded:Connect(function(player)
print("Player joined: " .. player.Name)
loadPlayerData(player)
end)
-- Player Removing Event
game.Players.PlayerRemoving:Connect(function(player)
print("Player leaving: " .. player.Name)
savePlayerData(player)
end)