How do I save both leaderstats and non-leaderstats values in the same table?

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

Thank you!

1 Like

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)

1 Like

maybe not now because that would be extra time and i don’t need an overly advanced system for my datastores

1 Like

alr ill take a look at it in a bit

1 Like

It’s free and very unique. Just read carefully about Key API

1 Like

its stressful trying to set this up since the plugin won’t download properly

1 Like

I have question. Is this “e” printing or not?

1 Like

Look at this:

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.

1 Like

nope, it is not printing and about your other post, ill look into it in a sec

1 Like

I think you shouldn’t use return 2 times. Try deleting first one

1 Like

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

1 Like

Would this tutorial be good to save leaderstats values where i try to convert the leaderstat value into a table value when saving

1 Like

Yes. Also it’s easy to convert it

Data[plr.uniqId].Cash = playerData.Coins

1 Like

alr i’ll try and do that then and see if it works

1 Like

i can’t really do that for some reason, are datastores hard to do

actually i’ll use this tutorial to remake my script from ground up, since its a more general tutorial and includes leaderstats

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

1 Like

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

fixed now (filler words for submission)