What do you want to achieve? Keep it simple and clear!
leader stats that save
What is the issue? Include screenshots / videos if possible!
I have written a script to save the leader stat “rebirths” but it does not work when I test it in studio, or in the actual game.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Video tutorials and dev forum
saving script (serverscriptservice) not local
local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local key = "user_"..plr.UserId
local savevalue1 = plr.leaderstats.Rebirths
local GetSaved = ds:GetAsync(key)
if GetSaved then
savevalue1.Value = GetSaved[1]
else
local valuesForSaving = {savevalue1.value}
ds:GetAsync(key, valuesForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local savevalue1 = plr.leaderstats.Rebirths
ds:SetAsync("user_"..plr.UserId, {savevalue1.Value})
end)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetChildren()) do
plr:Kick()
end
end)
leaderstats script (serverscriptservice) and not local
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”) --making leaderstats
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
I just came here to answer the same question, it was working for me but then broke, so i changed something and now it doesnt save.
local DataStoreService = game:GetService("DataStoreService")
local DATASTORE_NAME = "3_TheDataStoreSave"
local DEFAULT_STATS = {
Clicks = 0,
Coins = 0
}
local function createPlayerData(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stats = Instance.new("Folder")
stats.Name = "Stats"
stats.Parent = player
local clicksStat = Instance.new("IntValue")
clicksStat.Name = "Clicks"
clicksStat.Value = DEFAULT_STATS.Clicks
clicksStat.Parent = leaderstats
local coinsStat = Instance.new("IntValue")
coinsStat.Name = "Coins"
coinsStat.Value = DEFAULT_STATS.Coins
coinsStat.Parent = leaderstats
local worldStat = Instance.new("IntValue")
worldStat.Name = "World"
worldStat.Value = 1
worldStat.Parent = stats
local clickMultiplierStat = Instance.new("IntValue")
clickMultiplierStat.Name = "ClickMultiplier"
clickMultiplierStat.Value = 1
clickMultiplierStat.Parent = stats
end
game.Players.PlayerAdded:Connect(function(player)
local success, data = pcall(function()
return DataStoreService:GetDataStore(DATASTORE_NAME):GetAsync(tostring(player.UserId))
end)
if success then
if data then
local leaderstats = player:FindFirstChild("leaderstats")
local stats = player:FindFirstChild("Stats")
if leaderstats and stats then
leaderstats.Clicks.Value = data.Clicks or DEFAULT_STATS.Clicks
leaderstats.Coins.Value = data.Coins or DEFAULT_STATS.Coins
stats.World.Value = data.World or 1
stats.ClickMultiplier.Value = data.ClickMultiplier or 1
else
-- If the required folders don't exist, create a new data structure
createPlayerData(player)
end
else
createPlayerData(player)
end
else
warn("Failed to load player data for", player.Name, ":", data)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
local stats = player:FindFirstChild("Stats")
if leaderstats and stats then
local data = {
Clicks = leaderstats.Clicks.Value,
Coins = leaderstats.Coins.Value,
World = stats.World.Value,
ClickMultiplier = stats.ClickMultiplier.Value
}
local success, result = pcall(function()
DataStoreService:GetDataStore(DATASTORE_NAME):SetAsync(tostring(player.UserId), data)
end)
if not success then
warn("Failed to save player data for", player.Name, ":", result)
end
end
end)
I am not sure if this will help you, but I figured out my script:
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("V6_DataStoreSavingSave")
local debounceDelay = 10 -- Adjust this value as needed
local function getPlayerData(player)
local success, data = pcall(function()
return ds:GetAsync(player.UserId)
end)
if success then
return data or {
Coins = 0,
Clicks = 0,
ClickMultiplier = 1,
World = 1
}
else
warn("Failed to retrieve data for player " .. player.Name .. ": " .. tostring(data))
return {
Coins = 0,
Clicks = 0,
ClickMultiplier = 1,
World = 1
}
end
end
local function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
local stats = player:FindFirstChild("Stats")
if leaderstats and stats then
local data = {
Coins = leaderstats.Coins.Value,
Clicks = leaderstats.Clicks.Value,
ClickMultiplier = stats.ClickMultiplier.Value,
World = stats.World.Value
}
local success, err = pcall(function()
ds:SetAsync(player.UserId, data)
end)
if not success then
warn("Failed to save data for player " .. player.Name .. ": " .. tostring(err))
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new("Folder", player)
leader.Name = "leaderstats"
local Stats = Instance.new("Folder", player)
Stats.Name = "Stats"
local playerData = getPlayerData(player)
local Coins = Instance.new("NumberValue", leader)
Coins.Name = "Coins"
Coins.Value = playerData.Coins
local Clicks = Instance.new("NumberValue", leader)
Clicks.Name = "Clicks"
Clicks.Value = playerData.Clicks
local ClickMultiplier = Instance.new("NumberValue", Stats)
ClickMultiplier.Name = "ClickMultiplier"
ClickMultiplier.Value = playerData.ClickMultiplier
local World = Instance.new("NumberValue", Stats)
World.Name = "World"
World.Value = playerData.World
local debounce = Instance.new("BoolValue")
debounce.Name = "SaveDebounce"
debounce.Parent = player
local function saveData()
if debounce.Value == false then
debounce.Value = true
savePlayerData(player)
wait(debounceDelay)
debounce.Value = false
end
end
Coins.Changed:Connect(saveData)
Clicks.Changed:Connect(saveData)
ClickMultiplier.Changed:Connect(saveData)
World.Changed:Connect(saveData)
saveData()
end)
game.Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)