Need help with inventory system

i want to make same items in the backpack have a number on top instead of having two of the same thing.
my code:
`
local backpack = game.Players.LocalPlayer:WaitForChild(“Backpack”)
local inventory = game.Players.LocalPlayer.PlayerGui:WaitForChild(“inventory”)
local gui = inventory:WaitForChild(“maingui”)
local items = gui:WaitForChild(“items”)
local template = game.ReplicatedStorage:WaitForChild(“template”)
local player = game.Players.LocalPlayer
local function update()
local setup = items:WaitForChild(“setup”)
setup.Parent = gui
items:ClearAllChildren()
setup.Parent = items
for i,v in pairs(backpack:GetChildren()) do
local item = v
local name = item.Name
local clone = template:Clone()
local templatename = clone:WaitForChild(“name”)
templatename.Text = name
clone.Parent = items
print(name)
end
end

function characterAdded()
backpack = player:WaitForChild(“Backpack”)
backpack.ChildAdded:Connect(update)
end

if player.Character then characterAdded() end
player.CharacterAdded:Connect(characterAdded)

`

1 Like
local replicated = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local backpack = player:WaitForChild("Backpack")
local playerGui = player:WaitForChild("PlayerGui")
local inventory = playerGui:WaitForChild("inventory")
local mainGui = inventory:WaitForChild("maingui")
local items = mainGui:WaitForChild("items")
local setup = items:WaitForChild("setup")

local template = replicated:WaitForChild("template")

local function updateGui()
	setup.Parent = mainGui
	items:ClearAllChildren()
	setup.Parent = items
	
	for _, tool in ipairs(backpack:GetChildren()) do
		local templateClone = template:Clone()
		local cloneName = templateClone:WaitForChild("name")
		cloneName.Text = tool.Name
		templateClone.Parent = items
	end
end

backpack.ChildAdded:Connect(updateGui)

Not exactly sure what you’re trying to do with this script but here are the necessary fixes.

the script works but ineed like a little number to pop up if you have two of the same item

Where exactly should the number appear?

what you should do is insert an IntValue in the player somewhere (would suggest in a folder) whenever a player picks up something and set its value to 1, then when the player picks up the same item, check if there is a value inside the player, if there is then increase its value by 1 (use some sort of gui that updates accordingly)

local players = game:GetService("Players")
local player = players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function countTools(child)	
	local toolCount = 0
	for _, tool in ipairs(backpack:GetChildren()) do
		if tool.Name == child.Name then
			toolCount += 1
		end
	end
	print(player.Name.." has "..toolCount.." copies of the tool named "..child.Name.." in their backpack.")
end

backpack.ChildAdded:Connect(countTools)

This is how you should count the number of copies of a tool whenever a tool is added to the player’s backpack.

well, it clones multiple versions of the template which is annoying