Script keeps duplicating Camera and Parts in ViewPortFrame where it shouldn't

Hello, having a bit irritating problem. I have a 2 for loops to create ViewPortFrames for showing items in a GUI. The first for loop has 11 items to check so it loops 11 times. The second for loop has 2 items.

Example of the two for loops.

for i = 1,numberOfItems,1 do 
	if i == 1 then 
		box = item1
	else 
		
		box = item1:Clone()
		box.Name = "Item"..i
		box.Parent = itemFrame
		
		
		if (i-1) / (4*numRows) == 1 then
			-- New Row
			numRows = numRows + 1 
			box.Position = UDim2.new(PADDING_X,0,box.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_Y*(numRows - 1))
		else
			--Add to the X only
			box.Position = itemFrame["Item"..(i-1)].Position + UDim2.new(DROPDOWN_X,0,0,0)
		end
	end
			--- Creating Camera and Inserting into VPF for Items	---
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = box.VPF
	local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
	handle.Parent = box.VPF
	box.VPF.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
	itemFrame["Item"..i].ItemName.Text = availableTools[i][1]
		
for ib = 1,numberOfBuddys,1 do
		if ib == 1 then
			Bbox = budItem1
		else
			Bbox = budItem1:Clone()
			Bbox.Name = "Bud"..ib
			Bbox.Parent = buddyFrame

			if (ib-1) / (4*numRowsB) == 1 then 
				-- New Row 
				numRowsB = numRowsB + 1 
				Bbox.Position = UDim2.new(PADDING_XB,0,Bbox.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_YB*(numRowsB - 1))
			else 
				-- Adding to X only 
				Bbox.Position = buddyFrame["Bud"..(ib-1)].Position + UDim2.new(DROPDOWN_XB,0,0,0)
			end
		end
                 --- Creating Camera and Inserting into VPF for Buddys	---
	local budFakeCam = Instance.new("Camera")
	budFakeCam.Parent = Bbox.VPF
	local handleb = game.ReplicatedStorage:WaitForChild("BuddyModels"):FindFirstChild(availableBuddys[ib][1].."Pet"):Clone()
	handleb.Parent = Bbox.VPF
	Bbox.VPF.CurrentCamera = budFakeCam
	budFakeCam.CFrame = handleb.CameraCFrame.Value
	buddyFrame["Bud"..ib].BItemName.Text = availableBuddys[ib][1]

When it loops 11 times it creates 11 cameras and ViewPortFrame Buttons for each gui section. It should only be creating 11 for the numberOfItems Loop and 2 for the numberOfBuddys Loop. Is there a way to set a condition for the 2nd loop to determine how many Cameras and Buttons are cloned?

Could you share the exact script? I don’t see any “end” for any of the loops

for i = 1,numberOfItems,1 do 
	if i == 1 then 
		box = item1
	else 
		
		box = item1:Clone()
		box.Name = "Item"..i
		box.Parent = itemFrame
		
		
		if (i-1) / (4*numRows) == 1 then
			-- New Row
			numRows = numRows + 1 
			box.Position = UDim2.new(PADDING_X,0,box.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_Y*(numRows - 1))
		else
			--Add to the X only
			box.Position = itemFrame["Item"..(i-1)].Position + UDim2.new(DROPDOWN_X,0,0,0)
		end
	end
			--- Creating Camera and Inserting into VPF for Items	---
	local fakeCam = Instance.new("Camera")
	fakeCam.Parent = box.VPF
	local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
	handle.Parent = box.VPF
	box.VPF.CurrentCamera = fakeCam
	fakeCam.CFrame = handle.CameraCFrame.Value
	itemFrame["Item"..i].ItemName.Text = availableTools[i][1]
		
for ib = 1,numberOfBuddys,1 do
		if ib == 1 then
			Bbox = budItem1
		else
			Bbox = budItem1:Clone()
			Bbox.Name = "Bud"..ib
			Bbox.Parent = buddyFrame

			if (ib-1) / (4*numRowsB) == 1 then 
				-- New Row 
				numRowsB = numRowsB + 1 
				Bbox.Position = UDim2.new(PADDING_XB,0,Bbox.Position.Y.Scale,0) + UDim2.new(0,0,DROPDOWN_YB*(numRowsB - 1))
			else 
				-- Adding to X only 
				Bbox.Position = buddyFrame["Bud"..(ib-1)].Position + UDim2.new(DROPDOWN_XB,0,0,0)
			end
		end
		--- Creating Camera and Inserting into VPF for Buddys	---
	local budFakeCam = Instance.new("Camera")
	budFakeCam.Parent = Bbox.VPF
	local handleb = game.ReplicatedStorage:WaitForChild("BuddyModels"):FindFirstChild(availableBuddys[ib][1].."Pet"):Clone()
	handleb.Parent = Bbox.VPF
	Bbox.VPF.CurrentCamera = budFakeCam
	budFakeCam.CFrame = handleb.CameraCFrame.Value
	buddyFrame["Bud"..ib].BItemName.Text = availableBuddys[ib][1]

box.MouseButton1Down:Connect(function()
		for _, v in pairs(itemViewport:GetChildren()) do
			if not v:isA("Frame") then
				--print("Registered on Mobile Devices") 
				v:Destroy()
			end
		end
		local itemViewportCam = Instance.new("Camera")
		itemViewportCam.Parent = itemViewport
		
		local handle = game.ReplicatedStorage:WaitForChild("ToolModels"):FindFirstChild(availableTools[i][1].."Handle"):Clone()
		handle.Parent = itemViewport
		
		itemViewport.CurrentCamera = itemViewportCam
		itemViewportCam.CFrame = handle.CameraCFrame.Value 
		
		local owned = game.ReplicatedStorage.ItemCheck:InvokeServer(availableTools[i][1])
		
		if equippedItem.Value == availableTools[i][1] then
			infoFrame.Cash.Text = "Owned"
			infoFrame.BuyButton.Text = "Unequip"
		elseif owned == true then
			infoFrame.Cash.Text = "Owned"
			infoFrame.BuyButton.Text = "Equip"
		 else
			infoFrame.BuyButton.Text = "Buy"
			infoFrame.Cash.Text = "$"..availableTools[i][2]
		end
		
		infoFrame.ItemName.Text = availableTools[i][1]
		selectedItem.Value = availableTools[i][1]
		
		for _, v in pairs(itemFrame:GetChildren()) do 
			if v:IsA("ImageButton") then
				v.BorderSizePixel = 0
			end
		end
		
		itemFrame["Item"..i].BorderSizePixel = 2
	end)

Bbox.MouseButton1Down:Connect(function()
		for _, v in pairs(budItemViewPort:GetChildren()) do
			if not v:isA("Frame") then
				--print("Registered on Mobile Devices") 
				v:Destroy()
			end
		end
		local bitemViewportCam = Instance.new("Camera")
		bitemViewportCam.Parent = budItemViewPort

		local budMesh = game.ReplicatedStorage:WaitForChild("BuddyModels"):FindFirstChild(availableBuddys[ib][1].."Pet"):Clone()
		budMesh.Parent = budItemViewPort

		budItemViewPort.CurrentCamera = bitemViewportCam
		bitemViewportCam.CFrame = budMesh.CameraCFrame.Value 
			
		
		local bowned = game.ReplicatedStorage.BuddyCheck:InvokeServer(availableBuddys[ib][1])
			
	if budEquippedItem.Value == availableBuddys[ib][1] then
			
			budInfoFrame.Cash.Text = "Owned"
			budInfoFrame.BuyButton.Text = "Unequip"
			elseif bowned == true then

			budInfoFrame.Cash.Text = "Owned"
			budInfoFrame.BuyButton.Text = "Equip"
		else
			budInfoFrame.BuyButton.Text = "Buy"
			budInfoFrame.Cash.Text = "$"..availableBuddys[ib][2]
		end

		budInfoFrame.BItemName.Text = availableBuddys[ib][1]
		budSelectedItem.Value = availableBuddys[ib][1]

		for _, v in pairs(buddyFrame:GetChildren()) do 
			if v:IsA("ImageButton") then
				v.BorderSizePixel = 0
			end
		end

			buddyFrame["Bud"..ib].BorderSizePixel = 2
				
	end)

	end 
end

It seems like you are having the 2 iterations loop inside of the main loop, and also are connecting all the signals on the 2 iterations loop, is this correct? Maybe I am mistaken about this

2 Likes

both of the loops end at the same place. Would it be better practice to have both loops seperate?

My bad for not reading this, I apologize. Having 2 separate loops will work since you will only iterate 11 times for numberOfItems and 2 for numberOfBuddys, as you had the Buddys loops inside the items one, that code was executing 22 times instead of 2

1 Like

Its all good. I just separated the loops and now it works as intended. thanks for your help!

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