i’d want to show some saved values on the leaderstats such as cash and XP but not stuff like weapons owned or weapons equipped, etc.
this is because my game updates values via the leaderstats but those values are set up when the player joins outside the save system, and i want the leaderstats to be added and saved directly in the table with the other saved data without using variables to ‘represent’ the leaderstats.
My script below can load data, but it can’t save them for some reason. Could you help with that if possible
Here is the script:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
-- Getting scripts and events
local database = DataStoreService:GetDataStore("database")
local data = {}
-- Loads player's stats
local function loadData(player)
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId) -- Attempting to retrieve player data from Roblox Datastores
end)
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 5
if success then
print("Data retrieved")
if not playerData then
print("New player, giving default data")
playerData = {
["Cash"] = 20,
}
end
data[player.UserId] = playerData
else
warn("Unable to get data for player ", player.UserId)
player:Kick("There was a problem when getting your data, try rejoin the game")
end
end
Players.PlayerAdded:Connect(function(player)
loadData(player)
-- Creates leaderboard and its stats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Cash"
money.Value = data[player.UserId].Cash
money.Parent = leaderstats
end)
-- Saves player's stats
local function SaveData(player)
print("EE")
if data[player.UserId] then
local success = nil
local playerData = nil
local attempt = 1
-- data[player.UserId].Cash = player.leaderstats.Cash.Value
repeat
success, playerData = pcall(function()
return database:UpdateAsync(player.UserId, function()
return data[player.UserId]
end)
end)
print("e")
attempt += 1
if not success then
warn(playerData)
task.wait()
end
until success or attempt == 5
if success then
print("Data saved successfully")
else
warn("Unable to save data for player" .. player.UserId)
end
else
warn("No session data for ", player.UserId)
end
print("e")
end
Players.PlayerRemoving:Connect(SaveData)
When I run this, it successfully retrieves data and gives me default data (20 Cash) and when I leave the game, it prints “EE” with the ‘SaveData’ function but not anything else, which is strange.
So how do I fix this script in the want I want (leaderstats values saved as a value in playerData table) or give an alternative if this isn’t possible
I got problems with datastores too. I recommend using Super Biz. It takes time to configurate it but after you can look at your data values, change them, try some methods and etc. It was super useful for me and I understood how to architecture my datastore code
Edit: Example. Time was spent to configure 15 minutes(I’m just dumb, it could take 5 minutes)
I think your code is for database not leaderstats. Leaderstats are built-in Roblox feature. I believe they are saving automatically
Edit: @RakisahNew
I think you need to simplify your code because if all you need is Leaderstats then use Roblox built-in feature.
im planning to save both leaderstats and non-leaderstats values, do i need to save them separately since leaderstats values cannot use UpdateAsync according to the post
edit: i used a tutorial to update non-leaderstats values which is why my code is that way, specifically GnomeCode’s Tower Defense Tutorial Episode #22
The leaderstats folder is the default used by Roblox to make a leaderboard ingame. As long as you instantiate a folder of such name it will function.
For neatness simply save the table in the format
local save = {{leaderstats here}, {non-leaderstats here}}
And you’ll be able to access all the necessary data from one tuple
Ok I’ve used this tutorial but it cannot save data for some reason and doesn’t even return an error msg, heres the script
local function SaveData(player)
print("E")
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
print("E")
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
print("E")
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
print("E")
if success then
print(player.UserId .. "'s Data saved successfully")
else
warn("Unable to save sessionData for player" .. player.UserId)
end
else
warn("No session sessionData for ", player.UserId)
end
end
Players.PlayerRemoving:Connect(SaveData)
It only prints the first 2 Es in the SaveData function but nothing else