The goal of this script is to create a new label for each brick in a certain folder. After creating the label, since I don’t want them to be all at the same position, I’m trying to make their position a little lower than the previous label that was made. I tried this but it just results in this:
local TextLabel = script.Parent.ScrollingFrame.ReallyRed
local BricksFolder = game.Workspace:FindFirstChild("Bricks")
for _, child in ipairs(BricksFolder:GetChildren()) do
local newLabel = TextLabel:Clone()
newLabel.Text = child.Name
newLabel.Parent = script.Parent.ScrollingFrame
newLabel.Name = child.Name
if child.Name == "ReallyRed" then
newLabel:Destroy()
end
if newLabel.Parent == nil then
print("No parent found for".. child.Name)
else
--make it so every new label is a little below each other
local lastLabel = newLabel.Parent:FindFirstChild(child.Name)
if lastLabel == nil then
newLabel.Position = UDim2.new(0, 0, 0, 0)
else
newLabel.Position = UDim2.new(0, 0, 1.5 * lastLabel.Position.Y.Scale + .05 , 0)
end
end
end
I am guessing it is because FindFirstChild is giving you the text on top, not the one below it. Here is what I do in a similar script:
local ScaleNumber = 0
for i, v in pairs(newlabel.Parent:GetChildren()) do
if v:IsA("TextLabel") then
if v.Position.Y.Scale > ScaleNumber then --Checks if the TextLabel has a larger scale than the ScaleNumber variable
ScaleNumber = v.Position.Y.Scale --Increases the ScaleNumber variable
end
end
end
newLabel.Position = UDim2.new(0, 0, 1.5 * ScaleNumber + .05 , 0) --You might need to do 1.5 + ScaleNumber, but I'm not sure
I probably messed up the script somewhere, but hopefully you get the general idea of how it works.
This causes all the labels to end up in the same spot.
If you’re only creating the labels once, you can fix this by just changing this to:
-- Added a lastChild variable
lastChild = nil
for index, child in ipairs(BricksFolder:GetChildren()) do
local newLabel = TextLabel:Clone()
newLabel.Text = child.Name
newLabel.Parent = script.Parent.ScrollingFrame
newLabel.Name = child.Name
if child.Name == "ReallyRed" then
newLabel:Destroy()
end
if newLabel.Parent == nil then
print("No parent found for".. child.Name)
else
--make it so every new label is a little below each other
if lastLabel == nil then
newLabel.Position = UDim2.new(0, 0, 0, 0)
else
-- If it's not the first item position it by the previous
if lastChild then
-- Gets the label for the last child
local lastLabel = newLabel.Parent:FindFirstChild(lastChild.Name)
newLabel.Position = UDim2.new(0, 0, 1.5 * lastLabel.Position.Y.Scale + .05 , 0)
else -- Otherwise it's the first item so put it at the first position
newLabel.Position = UDim2.new(0, 0, 0, 0)
end
end
end
-- Update the last child variable
lastChild = child
end