Value resets to 0 after a for, i loop

I’m currentley working on a rudimentary inventory system. The way I’m showing duplicates is using a “stack” feature where the duplicates are shown through an amt on a textlabel. However, I’m encountering the issue of random tools amounts being set to 0, and was wondering if there is a script error or something about the loops I don’t know

Here’s my current code;

local function setupInventory()
	local inventoryTable = inventoryRem:InvokeServer()
	local cloneInv = table.clone(inventoryTable)
	if inventoryTable then
		for i, tool in inventoryTable do
			if not table.find(alreadyInserted, tool)::number then
				table.insert(alreadyInserted, tool)
				local newTemp = template:Clone()
				newTemp.Name = tool
				newTemp:WaitForChild("EquipBtn"):WaitForChild("btnHandler").Enabled = true
				local fakeTool = sM:WaitForChild(tool):Clone()
				newTemp.ToolName.Text = fakeTool.Name
				fakeTool.Parent = newTemp.ViewportFrame
				fakeTool.Position = goodPos
				newTemp.Parent = holder
			end
			if table.find(alreadyInserted, tool)::number then
				local frame = holder:WaitForChild(tool)
				local amt = 0
				for i, object in cloneInv do
					if object == tool then
						amt += 1
						table.remove(cloneInv, table.find(cloneInv, object))
						print("removed "..object.." amt is now "..amt)
					end
				end
				print(amt)
				frame.ItemCount.Value = amt
			end
		end
	end
end

Here is the output from that code;

 00:36:18.013  removed Sword amt is now 1  -  Client - inventoryHandler:45
  00:36:18.013  1  -  Client - inventoryHandler:48
  00:36:18.014  removed Premium amt is now 1  -  Client - inventoryHandler:45
  00:36:18.014  1  -  Client - inventoryHandler:48
  00:36:18.015  removed Xanwood amt is now 1  -  Client - inventoryHandler:45
  00:36:18.015  removed Xanwood amt is now 2  -  Client - inventoryHandler:45
  00:36:18.016  removed Xanwood amt is now 3  -  Client - inventoryHandler:45
  00:36:18.016  3  -  Client - inventoryHandler:48
  00:36:18.017  removed Black Iron amt is now 1  -  Client - inventoryHandler:45
  00:36:18.017  1  -  Client - inventoryHandler:48
  00:36:18.017  0  -  Client - inventoryHandler:48
  00:36:18.018  removed Bluesteel amt is now 1  -  Client - inventoryHandler:45
  00:36:18.018  1  -  Client - inventoryHandler:48
  00:36:18.018  0  -  Client - inventoryHandler:48

I have tried using many ideas but none really worked. Thanks in advance for the help :slight_smile:

3 Likes

Probably has something to do with the new tools, because if the script is advancing the amt everytime a same item is removed, then it might be possible that the amt count resets to 0 every time a new item is removed.

2 Likes

I’m currentely trying it with a new table for storing amt of each tool, I’ll lyk how that goes

2 Likes

Okay. Reply to me when ready. :smile:

2 Likes

Unfortunately didn’t work, I’m not sure what I’m doing wrong atp.

Here’s the updated code;

local function setupInventory()
	local inventoryTable = inventoryRem:InvokeServer()
	local cloneInv = table.clone(inventoryTable)
	local toolCounts = {}  -- New table to store tool counts

	if inventoryTable then
		for i, tool in ipairs(inventoryTable) do
			if not table.find(alreadyInserted, tool) then
				table.insert(alreadyInserted, tool)
				local newTemp = template:Clone()
				newTemp.Name = tool
				newTemp:WaitForChild("EquipBtn"):WaitForChild("btnHandler").Enabled = true
				local fakeTool = sM:WaitForChild(tool):Clone()
				newTemp.ToolName.Text = fakeTool.Name
				fakeTool.Parent = newTemp.ViewportFrame
				fakeTool.Position = goodPos
				newTemp.Parent = holder
			end

			if table.find(alreadyInserted, tool) then
				local frame = holder:WaitForChild(tool)
				local amt = 0

				local newCloneInv = {}
				for i, object in ipairs(cloneInv) do
					if object == tool then
						amt = amt + 1
					else
						table.insert(newCloneInv, object)
					end
				end
				cloneInv = newCloneInv
				toolCounts[tool] = amt
				print("Count for " .. tool .. " is " .. amt)
				frame.ItemCount.Value = amt
			end
		end
	end
end
2 Likes

Why not use ipairs? For this purpose, I think you will have more reliable and consistent results with this kind of loop.

2 Likes

Good point, I’ll try that now and get back to you
Unfortunately still the same issue, although if that is better for optimization then that’s good :+1:

2 Likes

I FOUND A FIX!!!
I had to move the second for loop outside of the first one to prevent number issues!!!

3 Likes

Hah. Simple problems require simple solutions. Glad you figured it out :+1:

2 Likes

Alternatively use a different index than “i” for loops within loops. You can use any letter or string in place of i

3 Likes

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