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.)
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.
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.
A VFrame goes as a child of one of the 4 slots shown in the image as textbuttons.
They get copied from…
…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.
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.