GUI Not deleting itself with scripts

Need to make the UI delete itself

–all of this is in a module script if that can help)

The gui clones itself over and over but it wont delete itself when I open the shop
Screenshot 2024-08-10 052058

I have tried to find solutions but can’t seem to find good and matching ones

here’s the script:

local shopGUI = script.Parent:FindFirstChild(“ShopGui”)
local frame = shopGUI.Frame
local container = frame.ContainerFrame

local itemTemplate = script.Template

local Shop = {}
Shop.__index = Shop

local function clearContainer()
for _, child in ipairs(container:GetChildren()) do
if child:IsA(“TextButton”) then
child:Destroy() --Won’t destroy the gui
end
end
end

function Shop.new(shop: string)
clearContainer()
local list = if shop == “Food” then toolConfig else toolConfig

1 Like

Hard to say without additional context, but if you replace

if child:IsA("TextButton") then

with

if child:IsA("GuiObject") then

does it work as desired?

2 Likes

or maybe instead of

for _, child in ipairs(container:GetChildren()) do

change ipairs to pairs and :GetChildren() to :GetDescendants()

for _, child in pairs(container:GetDescendants()) do
2 Likes

I always asked which one should i use, ipairs or pairs, what’s the difference between those two?

1 Like

You shouldn’t use any of them. Generalised iteration is the new kid in town.

for i, v in table do
2 Likes

ipairs guarantees orders, pairs doesnt. but like @bytesleuth said, you shouldnt use them anymore, generalised iteration is the new thing (thanks for letting me know lmaooo i didnt even know)

if you dont know already, Instance:GetDescendants() returns a table

2 Likes

try this:

local shopGUI = script.Parent:FindFirstChild("ShopGui")
local frame = shopGUI.Frame
local container = frame.ContainerFrame

local itemTemplate = script.Template

local Shop = {}
Shop.__index = Shop

local function clearContainer()
    for _, child in ipairs(container:GetChildren()) do
        if child:IsA("TextButton") then
            child:Destroy()
        end
    end
end

function Shop.new(shop: string)
    clearContainer()
    
    local list = (shop == "Food") and toolConfig or toolConfig

    for _, item in ipairs(list) do
        local newItem = itemTemplate:Clone()
        newItem.Parent = container
        newItem.Text = item.Name
    end
end

function Shop:open()
    shopGUI.Enabled = true
end

function Shop:close()
    shopGUI.Enabled = false
    clearContainer()
end

return Shop

I haven’t been on this forum for a while but I found the problem, I forgot where it was exactly but I think it was a misspell on a capital, thank you all for your time.

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