How to save many IntValues in datastore2

Currently, i’m trying to create an inventory system with IntValues representing how many of each items the player has. However, there will be many IntValues representing every item and I want to save them all.

Here’s an example:
Screenshot 2022-06-21 113815

Assuming there are about at least 50 IntValues, how would I save them all without writing each and every one of them?

1 Like

I don’t think there’s a way to do that. I know it might be tedious, but I think you have to write them all. You could write a basic code that each IntValue will use and copy and paste it then change it for each one to make it go by quicker.

You can do a loop to save all of them lol

3 Likes

For an example of what @SelfDeclared mentioned,

local InvetoryFolder = nil --Define the folder instance here
local InvertoryData = {}
for i,v in ipairs(InvetoryFolder:GetChildren()) do
	if v:IsA("ValueBase") then 
		InvertoryData[v.Name] = v.Value
	end
end
2 Likes

i have here sidesalad you can change it to inventory)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local runService = game:GetService("RunService")
local function onPlayerJoin(player)  -- Runs when players join
	
	local sidesalad = Instance.new("Folder")
	sidesalad.Name = "sidesalad"
	sidesalad.Parent = player

	local lvl = Instance.new("IntValue")
	lvl.Name = "lvl"
	lvl.Parent = sidesalad	
	
	local Str = Instance.new("IntValue")
	Str.Name = "Str"
	Str.Parent = sidesalad	

	local Mana = Instance.new("IntValue")
	Mana.Name = "Mana"
	Mana.Parent = sidesalad	

	local Health = Instance.new("IntValue")
	Health.Name = "Health"
	Health.Parent = sidesalad

	local Stamina = Instance.new("IntValue")
	Stamina.Name = "Stamina"
	Stamina.Parent = sidesalad

	local StatPoints = Instance.new("IntValue")
	StatPoints.Name = "StatPoints"
	StatPoints.Parent = sidesalad	

	local dr = Instance.new("IntValue")
	dr.Name = "dr"
	dr.Parent = sidesalad

	local exp = Instance.new("IntValue")
	exp.Name = "exp"
	exp.Parent = sidesalad

	local maxexp = Instance.new("IntValue")
	maxexp.Name = "maxexp"
	maxexp.Parent = sidesalad

	local class = Instance.new("StringValue")
	class.Name = "class"
	class.Parent = sidesalad

	--local function upgradestats()
	--	lvl.Value = lvl.Value + 1
	--	dr.Value = dr.Value + lvl.Value / 100
	--	print(player.Name,"Has just got Level up! And now he got",lvl.Value,"Lvl")
	--end

	--exp:GetPropertyChangedSignal("Value"):Connect(function()
	--	if maxexp.Value == 0 then
	--		maxexp.Value = 5
	--	end
	--	if  exp.Value == maxexp.Value then
	--		exp.Value = 0
	--		maxexp.Value = maxexp.Value * 1.25
	--		upgradestats()
	--	elseif exp.Value >= maxexp.Value then
	--		local a = Instance.new("IntValue")
	--		a.Name = "бебра"
	--		a.Parent = sidesalad
	--		a.Value = exp.Value
	--		exp.Value = 0
	--		while a.Value > maxexp.Value do
	--			if a.Value > maxexp.Value then
	--				a.Value = a.Value - maxexp.Value
	--				maxexp.Value = maxexp.Value * 1.25
	--				upgradestats()
	--			end
	--		end
	--		exp.Value = a.Value
	--		print("Your exp now = ",exp.Value)
	--		a:Destroy()
	--	end
	--end)



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then
		lvl.Value = data['lvl']
		Str.Value = data['Str']
		dr.Value = data['dr']
		class.Value = data['class']
		Health.Value = data['Health']
		Mana.Value = data['Mana']
		maxexp.Value = data["maxexp"]
		exp.Value = data['exp']
		Stamina.Value = data["Stamina"]
		StatPoints.Value = data["StatPoints"]
	else
		Health.Value = 1
		maxexp.Value = 5
		lvl.Value = 1
		StatPoints.Value = 3 -- sets the values to standart it's for not to have your lvl 0 or health 0
	end

end



local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.sidesalad:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end
	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')
		print(err)

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)



game:BindToClose(function()
	wait(10)
end)

``

Ohh, I don’t know how I didn’t think of this, thanks.

1 Like

My pleasure it’s great helping people.

1 Like