How can I script a text label that shows the amount of inventory storage

I have a simple inventory system with saving using datastore2 and I want to create a textlabel that shows the max inventory space and the amount used, eg 12/30. So far I have an intvalue inside a folder within the player. However Im confused on how to make a script that counts the amount of Frames inside a scrollingframe (Inventory).

And if using a stringvalue is the correct way I have a question: Should I change it on the server or client?

PlayerAdded ServerScript:

Players.PlayerAdded:Connect(function(player)
	local InventoryStore = DataStore2("Inventory", player)
	
	local replicatedDataFolder = Instance.new("Folder")
	replicatedDataFolder.Parent = ReplicatedStorage.ReplicatedData
	replicatedDataFolder.Name = player.UserId
	
	local InventoryString = Instance.new("StringValue")
	InventoryString.Parent = replicatedDataFolder
	InventoryString.Name = "Inventory"
	
	
	local StatsFolder = Instance.new("Folder", player)
	StatsFolder.Name = "StatsFolder"
	
	local StorageString = Instance.new("IntValue", StatsFolder)
	StorageString.Name = "StorageString"
	StorageString.Value = 0
	
	local InventoryData = InventoryStore:Get({})
	InventoryString.Value = HttpService:JSONEncode(InventoryData)
	
	
	InventoryStore:OnUpdate(function(decodedData)
		InventoryString.Value = HttpService:JSONEncode(decodedData)
	end)
	
	
	SendData:FireClient(player, InventoryData)
end)

The local script that creates an item inside the scrollingframe (Inventory) or adds it if it already exists:

local function CreateNewItemFrame(ItemName, amount)
	local ItemFrame = ReplicatedStorage.ItemFrame:Clone()
	local ItemRarity = ItemProps[ItemName].Rarity
	local RarityItem = RarityColours[ItemRarity]
	ItemFrame.Name = ItemName
	ItemFrame.Image = ItemProps[ItemName].ImageId
	ItemFrame.AmountLabel.Text = "x"..amount
	ItemFrame.ColourRarity.BackgroundColor3 = RarityItem
	ItemFrame.Parent = Container
	StatsString.Value += 1
end

local function UpdateItemFrame(ItemFrame, amount)
	ItemFrame.AmountLabel.Text = "x"..amount
	print("set amount")
end

SendData.OnClientEvent:Connect(function(Inventory)
	for ItemName, amount in pairs(Inventory) do
		local ItemFrame = Container:FindFirstChild(ItemName)

		if not ItemFrame then
			CreateNewItemFrame(ItemName, amount)
		else
			UpdateItemFrame(ItemFrame, amount)
		end

		if amount <= 0 then
			ItemFrame:Destroy()
		end
	end
end)

Thanks,
Sindious

Note: “ItemProperties” is a module script that contains properties for each item and I have a value inside that determines the amount of space that item takes. Basically I would like different items to take a different amount of space however Im unsure on how to make it so floats don’t round to a whole number.

local ItemProperties = require(script.Parent.ItemProperties)

local function AddToInventory(player, item, amount)
local replicatedDataFolder = player.UserId
local InventoryString = replicatedDataFolder.Inventory
local decodedInventory = HttpService:JSONDecode(InventoryString.Value)

local InventoryStore = DataStore2(“Inventory”, player)

if ItemProperties[item].stackable == true then
if decodedInventory[item] then
decodedInventory[item] = decodedInventory[item] + amount
InventoryStore:Set(decodedInventory)
else
decodedInventory[item] = amount
InventoryStore:Set(decodedInventory)
end
end

if ItemProperties[item].stackable ~= true then
if decodedInventory[item] then
decodedInventory[item] = decodedInventory[item] + 1
InventoryStore:Set(decodedInventory)
else
decodedInventory[item] = 1
InventoryStore:Set(decodedInventory)
end
end
end

return AddToInventory

The answer is:
local InventoryStorage = Inventory.ScrollingFrame.UIListLayout:GetChildren()
for _,v in pairs(InventoryStorage) do
StorageString.Value = StorageString.Value + ItemProperties[v.Name].amount
end

I did this however the loop at the bottom doesnt run

SendData.OnClientEvent:Connect(function(Inventory)
	for ItemName, amount in pairs(Inventory) do
		local ItemFrame = Container:FindFirstChild(ItemName)

		if not ItemFrame then
			CreateNewItemFrame(ItemName, amount)
		else
			UpdateItemFrame(ItemFrame, amount)
		end

		if amount <= 0 then
			ItemFrame:Destroy()
		end
	end
	local InventoryStorage = Container.UIGridLayout:GetChildren()
	for _, v in pairs(InventoryStorage) do
		print("loop")
		StatsString.Value = StatsString.Value + ItemProps[v.Name].Space
		StorageLabel.Text = StatsString.Value.."/"..MaxStorageString.Value
	end
end)

First off, that isn’t a loop at all because nowhere does the statement repeat.

I would change it to:

for _, v in pairs(InventoryStorage) do
	local num = #InventoryStorage
	StatsString.Value = StatsString.Value + ItemProps[v.Name].Space --change this
	StorageLabel.Text = StatsString.Value.."/"..MaxStorageString.Value --change this
end

I did that but Im unsure where to place the code

Not sure where you want it, but just send the .Text = tostring(num)

After some trial and error I got it to work:

local InventoryStorage = Container:GetChildren()
	
	for _, v in pairs(InventoryStorage) do
		if v:IsA("ImageButton") then
			local FrameName = v.Name
			local Space = ItemProps[FrameName].Space
			print(FrameName, Space)
			StatsString.Value = StatsString.Value + ItemProps[v.Name].Space
			StorageLabel.Text = tostring(StatsString.Value).."/"..MaxStorageString.Value
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.