How do i delete the top child in a list layout?

ive tried GetChildren()[2] (to ignore the layout) but that deletes the bottom child instead.
How do i get the top child on the layout and destroy it?
If you ant more detail please tell me

2 Likes
local modelWithParts = workspace.ModelWithParts

for i, v in ipairs(modelWithParts:GetChildren() do
    v:Destroy
    break
end

Try this and see if it works for you.

local parentFrame = script.Parent.Frame -- your frame here

local function getTopChildIgnoringLayout(frame)
	local children = frame:GetChildren()
	for _, child in ipairs(children) do
		if not child:IsA("UIListLayout") and not child:IsA("UIPadding") then
			return child
		end
	end
	return nil
end

-- Get the top child
local topChild = getTopChildIgnoringLayout(parentFrame)

-- Destroy the top child if it exists
if topChild then
	topChild:Destroy()
else
	print("No valid child found to delete.")
end
1 Like

thats destroying the list layout, im gonna try to add a “if not V:isA(“ListLayout”)”

1 Like

just tried it, didnt work its still deleting the bottom child

Probably would’ve helped had I re-read the post, lol. I totally misunderstood what your issue was. Yeah, go for @axcorpio’s script.

can you please optimize this rq to fit inside one local function?

i will try it (character limit)

i just tried and it didnt work, idk if that because i did it wonr or what so ill let you do it

local parentFrame = script.Parent.Frame -- your frame here

local function destroyTopChildIgnoringLayout(frame)
	local children = frame:GetChildren()
	for _, child in ipairs(children) do
		if not child:IsA("UIListLayout") and not child:IsA("UIPadding") then
			child:Destroy()
			return
		end
	end
	print("No valid child found to delete.")
end

-- Call the function
destroyTopChildIgnoringLayout(parentFrame)

Maybe this will work

1 Like

didnt work its still deleting the bottom child, it deletes the top cihld once then starts doing only the bottom when i call the function again

1 Like

Everything works for me. Maybe the problem is in frame or in chidlren?

Screenshots (it starts to delete childs, black frame is the lowest child):

Screenshot 2024-06-18 185056

Screenshot 2024-06-18 185059

1 Like

i am constantly adding new children, would that maybe be the issue?

1 Like

You are adding children after the function?

1 Like

I maded like i add new frame every time i run the function and like change i += 1:

while true do
	destroyTopChildIgnoringLayout(parentFrame)
	wait(0.5)
	local new = temp:Clone()
	new.Parent = parentFrame
	new.BackgroundColor3 = Color3.new(1, 0, 1)
	new.Name = i
	i += 1
end

And it still destroys the highest number

Screenshot 2024-06-18 185652

1 Like

no another part of the script already adds the child, i just need to fidn the top one and destroy it. Also all the object are all called line, and i cant get a system to make them numbered

1 Like
local function VisClear(count)
	local parentFrame = script.Parent.Frame.ScrollingFrame -- your frame here

	local children = parentFrame:GetChildren()
	for _, child in ipairs(children) do
		if not child:IsA("UIListLayout") and not child:IsA("UIPadding") then
			child:Destroy()
			return
		end
	end
	print("No valid child found to delete.")
end

local CanPlay = true

local function VisualizeSong(ID)
	if CanPlay == true then
		local Sond = Instance.new("Sound")
		Sond.Parent = script.Parent
		Sond.SoundId = "rbxassetid://"..ID
		task.wait(0.05)
		if Sond.IsLoaded == true then
			Sond:Play()
			CanPlay = false
		else
			PrintTo("Warn", "Unable to load ID")
			CanPlay = true
		end

		-- Visualize --
		local SoundWave = nil
		while Sond.Playing == true do
			task.wait()
			SoundWave = Sond.PlaybackLoudness
			local Step1 = math.round(SoundWave)
			local Step2 = tonumber(Step1) / 10
			local Step3 = string.rep("-", Step2)
			PrintTo("Normal", "|"..Step3..">")
			local count = #script.Parent.Frame.ScrollingFrame:GetChildren()
			if count > 20 then
				VisClear(count)
			end
		end
		CanPlay = true
	end
end

VisClear s the command im working on that clears the top child, but it wont work. PrintTo() is a function that adds to the list layout
the rest is just for audio visuals

Im checking if there are more than 20 children, if there are then i want to delete the top one. This would make the labels look like their flowing in an audio visualizer

local target = workspace:WaitForChild("Folder")

if target and #target:GetChildren() > 0 then
	target:GetChildren()[1]:Destroy()
end

Never tried this before, this seems to work while running. But when you exit the file is still there.
Not sure if that will work for you …

for -i, child in child:GetChildren() do
child:Destroy()

But it should always start from the top, thats weird