Trouble with deleting ViewportFrames

For some reason when I try to destroy all ViewportFrames from a TextButton, they don’t really get destroyed.
Here is a part of the code that I’m using.
Note: This function is in a ModuleScript and it only gets called from a GlobalScript.

local function changeUITroops(plr)
local UI = plr:FindFirstChild("PlayerGui"):FindFirstChild("TroopsUI")
if UI then
	local folder = UI.Frame.Frame
	local playerDeck = _G.PlayerDecks[plr.Name]
	if playerDeck then
		for i,v in pairs(playerDeck) do
			local vframe
			for l,p in pairs(folder:GetDescendants()) do
				if p.Name == "TroopFrame" then
					print("Deleting") 
					for y,u in pairs(p:GetChildren()) do
						u:Destroy()
					end
					p:Destroy()
				end
			end
			if playerDeck[i] then
				vframe = findVFrameByTroop(v)
				vframe = vframe:Clone()
				vframe.Parent = folder:FindFirstChild("Button"..tostring(i))
			end
		end
	end
end

end

Q: What is the game all about.
A: It is a simple tower defence game that I’m doing as a small project.

Q: What is the function used for?
A: It is meant to update (add and remove) ViewportFrames from the player’s slot UI.
(I’ve provided 2 Images of what I mean by that.)
image

image

All of the ViewportFrames are called “TroopFrame”.
Also, I do not get any errors when running the function. The part which says p:Destroy() just doesn’t seem to work for some reason.
The Function is used when a player wants to equip or unequip a troop from a ShopUI.
Also is there an alternative way of deleting something except using :Destroy() and :remove()?

Does It print (“Deleting”)? Also I don’t think this would cause the problem, but you don’t need to loop through the children of the TroopFrame. Deleting the Frame will clear all of it’s children.

Take out this:

Yes it does print the comment but for some reason it just doesn’t want to delete it. It doesn’t even give any errors.
Also, “p” is the ViewportFrame and “u” is the model troop in it.

Does “u” destroy, or does that not destroy either? So if you check the explorer the frame is still there?

I thought that it first needs it’s children destroyed so I destroy the children first and after that I destroy the VFrame
( p ).

So is the viewport under the TroopFrame? If it is can’t you just do

If p.Name == "TroopFrame" then
print("Deleting")
p.ViewportFrame:Destroy()
end

image

A VFrame goes as a child of one of the 4 slots shown in the image as textbuttons.

They get copied from…

image

…and are added as children in one of the buttons.
After the Equip and Unequip button is clicked it deletes all VFrames from those buttons and adds new ones.

Exactly so you would go

If p.Name == "TroopFrame" and p:FindFirstChild("ViewportFrame") then
p.ViewportFrame:Destroy()
end

Do you mean p:IsA(“ViewportFrame”) ? If you did I tried that out too.

Then why are you looping and destroying everything under the viewport when you are already just destroying it.

Because the idea is to destroy every vframe and replace them with the new updated ones.

I might have found the bug in it. When playerDeck has 0 arguments. It loops threw nothing. So when unequipping, value gets destroyed and playeDeck gets a table of 0 data and it passes the for delete loop.

Thanks for the help, I was able to figure it out somehow.