Create label using script

is there any way to create a text label in a frame for the amount of parts with the name ‘table’ in workspace?

1 Like

Use :GetDescendants() and iterate

local t = {}
for i, v in ipairs(workspace:GetDescendants()) do
    if v.Name == "table" then
        table.insert(t, v)
    end
end
2 Likes

You could make a for loop to go through all of the objects and implement if checks for the name. If you’re doing this on a server you should use a RemoteEvent. Also in the part where you say that you want to make a text label for the amount of parts? Do you mean if there are (for example) 10 parts you want 10 text labels or you just want 1 text label with text that would present the count of how many parts named table are there?

I mean like if there are 20 parts in workspace that have a name with ‘table1’ or ‘table2’, etc. to get those

Use this but instead of v.Name == "table", use v.Name:sub(1, 5) == "table"

The following would work, assuming you want to arbitrarily create a new TextLabel instance when this for-loop runs:

for i, v in pairs(game.Workspace:GetDescendants()) do
    if v.Name == table then
        local newLabel = Instance.new("TextLabel")
        newLabel.Name = " " -- Place the name of your new TextLabel in the quotations
        newLabel.Parent = -- Type the path to your GUI frame as the parent
    end
end

Now, if you already had a “template” for this TextLabel in somewhere such as ReplicatedStorage or PlayerGui, then you could clone it into the frame something like this:

local template = -- Type the path to your TextLabel template, in ReplicatedStorage or PlayerGui, etc.

for i, v in pairs(game.Workspace:GetDescendants()) do
    if v.Name == "table" then
        local labelClone = template:Clone()
        labelClone.Name = " " -- Place the name of your new TextLabel in the quotations
        labelClone.Parent = -- Type the path to your GUI frame as the parent
    end
end