Problem with for loop looping more than once

Excuse me if it’s a fairly stupid question, but I can’t seem to find what’s the issue.

I have a dictionary which I have my furniture in, along with its level and price. The dictionary is in a ModuleScript but I’m accessing it (currently) from a LocalScript to load the prices onto a GUI to make it more efficient for the future (future planning ftw!).

if Module then
	local Furniture = Module

	local tSize = 0

	for _, entry in pairs(Module) do
		tSize = tSize + 1
		print(tSize)
	end
	
	for _, furni in pairs(Furniture) do
		for i = 0, tSize do
			print(i)
			local ItemToClone = script.Parent.ItemBackground.ScrFrame.BuildItems.Item
			local Clone = ItemToClone:Clone()
			Clone.Parent = script.Parent.ItemBackground.ScrFrame.BuildItems
			Clone.Price.Text = furni.Price
		end
	end
end

This works OK but it generates a higher amount of clones than intended.

I have a feeling it has something to do with the dictionary because it may be seeing all 6 values currently in the table (fire station, level, price) & (police station, level, price).

tl;dr - Loop is looping more than once. It shouldn’t create that many clones. Possible reason is use of dictionary in demonstrated way above ^^. How to fix this? :slight_smile:

1 Like
if Module then
	local Furniture = Module

	local tSize = 0

	for _, entry in pairs(Module) do
		tSize = tSize + 1
		print(tSize)
	end
	
	for _, furni in pairs(Furniture) do
		--for i = 0, tSize do -- what is that loop for?
			print(i)
			local ItemToClone = script.Parent.ItemBackground.ScrFrame.BuildItems.Item
			local Clone = ItemToClone:Clone()
			Clone.Parent = script.Parent.ItemBackground.ScrFrame.BuildItems
			Clone.Price.Text = furni.Price
		--end
	end
end
1 Like

Thanks for correcting my horrible mistake yet again, I’ll learn next time :slight_smile:

1 Like

You’re welcome, good luck! :relaxed:

1 Like