Loop Creates Too Many Objects

Hello,

I am making a User Check game, which has a feature to check the user’s outfits. I have a menu for this feature, featuring the outfit’s image and name. Problem is, the loop that I call the function in, calls too many times.
Here’s a video of it:

Anyways, here’s the loop and the function:
Function:

local function createOutfitInfo(name,id)
	--for _, i in ipairs(id) do
	--	for _, n in ipairs(name) do
			local outFitImage = Instance.new("ImageLabel",outfitsBackgrounds)
			outFitImage.Image = "rbxthumb://type=Outfit&id=" .. id .. "&w=420&h=420"
			outFitImage.BackgroundTransparency = 1
			local nameTextLabel = Instance.new("TextLabel",outFitImage)
			nameTextLabel.TextColor3 = Color3.fromRGB(225,225,225)
			nameTextLabel.Position = UDim2.new(-0.3,0,1,0)
			nameTextLabel.Size = UDim2.new(0,99,0,33)
			nameTextLabel.TextScaled = true
			nameTextLabel.Text = name
			nameTextLabel.Name = name
-----------------------------------------------------------------------------------------------------------------------
			nameTextLabel.FontFace.Bold = true
			nameTextLabel.BackgroundTransparency = 1
			local spawnButton = Instance.new("TextButton",outFitImage)
			spawnButton.Size = UDim2.new(0,100,0,23)
			spawnButton.Position = UDim2.new(0,0,0.61,0)
			spawnButton.TextScaled = true
			spawnButton.TextColor3 = Color3.fromRGB(238,238,238)
			spawnButton.BackgroundColor3 = Color3.fromRGB(51,152,0)
			spawnButton.Text = "Spawn Outfit"
			spawnButton.FontFace.Bold = true
			spawnButton.Visible = false
			outFitImage.InputBegan:Connect(function(inp)
				if inp.UserInputType == Enum.UserInputType.MouseButton1 then
					spawnButton.Visible = not spawnButton.Visible
				end
			end)
	--	end
	--end
end

The Loop:

for inde,name in ipairs(outFitNames) do
					for index,id in ipairs(outFitIds) do
						coroutine.wrap(createOutfitInfo)(name,id)
						if index >= #outFitIds then
							break
						end
					end
					if inde >= #outFitNames then
						break
					end
				end

Help is appreciated!

I fixed it, using indexes out of all things…

for index,name in ipairs(outFitNames) do
	coroutine.wrap(createOutfitInfo)(name,outFitIds[index])
end

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