How to save value without it showing in leaderstats?

Hello! I’m new to scripting and i need some help with my datastore script. I would like to save value without it showing in leaderstats. It’s for simulator backpack so it can change the value whenever someone buys new backpack and gets more room for their “water”. I’ve watched many Youtube tutorials on how to make simulator backpack but there is no tutorial on how to save the backpack.
I just want to save value without it showing in the leaderstats and please say if you know better way to make backpack for simulator game.

Here is my script:

local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player)  

	local leaderstats = Instance.new("Folder")  

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player
	
	

	local money = Instance.new("IntValue") 

	money.Name = "Money"

	money.Parent = leaderstats



	local water = Instance.new("IntValue") 

	water.Name = "Water"

	water.Parent = leaderstats
	
	local backpack = Instance.new("IntValue") 
	
	
	backpack.Name = "Backpack"

	backpack.Parent = leaderstats
	

	local playerUserId = "Player_" .. player.UserId  

	local data = playerData:GetAsync(playerUserId)  

	if data then
		
		money.Value = data['Money']

		water.Value = data['Water']
		
		backpack.Value = data['Backpack']
	else
		
		money.Value = 0

		water.Value = 0
		
		backpack.Value = 0
		
	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value
		
	end

	return player_stats

end


local function onPlayerExit(player)

	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats)

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)
	

And i want to remove this from here:
leaderstats

1 Like

You can parent it to the Player, and do the same as you’d do with leaderstats.

And save it to data store.

3 Likes

Since anything inside the player’s leaderstats folder will show up (unless you hit the limit, which is 4), simply save it somewhere else, as someone else already mentioned.

There are plenty of places to store it, but having the value attached to the player instance itself would be a solid option, as you don’t need to do checks when the player leaves, as once the player instance is destroyed (ex. when they leave), its descendants will be destroyed too.

  • Useful information
    Leaderboards use value type objects to store and display player stats, nothing else.
    Another note: It will only show children of the leaderstats folder, not its descendants (ex. Player.leaderstats.Folder.BoolValue )
3 Likes

Make a folder inside leaderstats then add the value side.
Then update your code (I’ve already fixed it, the code is provided below)

Code
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

local function onPlayerJoin(player)  
	local leaderstats = Instance.new("Folder")  
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue") 
	money.Name = "Money"
	money.Parent = leaderstats

	local water = Instance.new("IntValue") 
	water.Name = "Water"
	water.Parent = leaderstats
	
	local backpackFold = Instance.new("Folder",leaderstats)
	backpackFold.Name = "Backpack"
	local backpack = Instance.new("IntValue") 
	backpack.Name = "Backpack"
	backpack.Parent =backpackFold

	local playerUserId = "Player_" .. player.UserId  
	local data = playerData:GetAsync(playerUserId)  
	
	if data then
		money.Value = data['Money']
		water.Value = data['Water']
		backpack.Value = data['Backpack']
	else
		money.Value = 0
		water.Value = 0
		backpack.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		if stat:IsA("Folder") then
			player_stats[stat.Name] = stat[stat.Name].Value
		else
			player_stats[stat.Name] = stat.Value
		end
	end
	return player_stats
end


local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)
	if not success then
		warn('Could not save data!')
	end
end



game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

image
image

@Syclya you are kinda wrong, not anything inside leaderstats will show.
Only Values so you can add Models, folders in leaderstats.

1 Like

I assumed you understood what I meant, as that is basic knowledge.
Leaderboards use value type objects to store and display player stats, nothing else.

Another note: It will only show children of the leaderstats folder, not its descendants (ex. Player.leaderstats.Folder.BoolValue)

1 Like

I understood because I am not new to scripting, I am talking about those who are a beginner at scripting. :+1:

I have edited my original post, enjoy.

(Though, most people just copy-paste scripts these days, so you are not wrong regarding new scripters have who no clue on how leaderstats are handled by Roblox’s engine.)

1 Like