I'm having trouble with a custom UI

Hello fellow Roblox Developers, I have come here because I do not have enough brain power to explain this to any of you. I am using code to get a list of the Text Buttons, and the same LocalScript always appears in the table when it shouldn’t be there. It isn’t appearing when I print the object name’s in the loop, and isn’t being removed by object:IsA(“LocalScript”).
image

image

image

Is “Slots” a table? You have to use :GetChildren() for directories. What Slots stands for?

1 Like

Have you tried:

local descendants = Hotbar:GetDescendants()
for _, descendants in pairs(descendants) do
if descendants:IsA(“TextButton”) then
print(descendants.Name)
end
end 
1 Like

why not just remove the or object:IsA("LocalScript") condition? It seems like the first condition would do the job.

2 Likes

Slots is a table, and already has :GetChildren() listed in the arguments.

table.unpack(Slots)

I added the condition after It didn’t work with the first condition, just to see if that was the issue. However if I do it backwards and instead of removing the items, I created a new table and add the TextButtons that are found, it works. Which is why I said I couldn’t explain it to anyone lol. I’d still rather not use this, even though it works. Just to try and loop through less objects to reduce delay.

image

Actually, I just remembered why the original doesn’t work.

It’s because you are trying to modify a table (Slots) while looping through it. It doesn’t work properly because it’s skipping over certain entries and then never reaching others. I’ve done what you’re trying to do by using a while loop, like so:

local function GetSlots(Slots)
    local i = 1
    while i <= #Slots do
        local object = Slots[i]
        if object:IsA("TextButton") then
            i += 1
        else
            table.remove(Slots, i)
          -- removing an item from the table moves the next one back in the list
          -- so we keep "i" as the same value on the next iteration of the loop
        end
    end
    return Slots
end

Old code (used OCR, please paste your code next time rather than pasting an image):

local function GetSlots(Slots)
    for i, object in Slots do -- don't need pairs() here 
        if not object:IsA("TextButton") then
            table.remove(Slots, i)
        end
    end
    return Slots
end
1 Like

I would just like to know one thing though, why don’t i need pairs()?

From May 2022 Luau Recap:

1 Like

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