Why does this script only if i make the numbers 20 and 19?


This script only works when i have the numbers at only 19 and 20, otherwise it breaks
No errors, but for some reason changing the numbers makes the newest child be destroyed, not the second oldest one. If i have the numbers at this specific value though it destroys the second oldest child.
Why cant i change the numbers
Heres the code in text format:

local function VisClear(A)
	local count = 0
	for _,v in pairs(script.Parent.Frame.ScrollingFrame:GetChildren()) do
		count = count + 1
	end
	print(count)
	if count == 20 then
		print("aaaaaa")
		script.Parent.Frame.ScrollingFrame:GetChildren()[2]:Destroy()
		count = 19
	end
end

The code is ran every frame

4 Likes

The “GetChildren” method returns an array of child instances in unordered manner. This means that it doesn’t matter when the child instance was created, it will be at a random index in this array.

2 Likes

thats why i have the [2] that gets the second child

1 Like

A few things. One, you can get the number of children in the scrolling frame by using a hashtag:

local count = #ScrollingFrame:GetChildren()

Also, what is the point for setting count to 19 if the function is not being used again? Also, what does 20 represent in this code?

1 Like

its constantly running this every frame so it resets the number after deleeting the child.
20 is just cecking if there are 20 children or more

1 Like

Use >= if you’re looking for 20 or more. If you’re using ==, you’re just gonna be checking if there’s just 20.

1 Like

ok well i changed my code to this:

local function VisClear(A)
	local count = #script.Parent.Frame.ScrollingFrame:GetChildren()
	print(count)
	if count == 30 then
		print("aaaaaa")
		script.Parent.Frame.ScrollingFrame:GetChildren()[2]:Destroy()
	end
end

and its still breaking if aset a higher number than 20. in this case the code is broken because the checking number is higher than 20

1 Like

You’re still only checking if the number is directly equal to 30, not more than or equal to… If it’s 29, it’s false. If it’s 30, it’s true. If it’s 31, then it’s false.

Use >= instead of ==.

1 Like

still doing the same thing. i donjt want to use the number 20 but i think im gonna have to and im gonna have to accept that my thing looks terrible

dude

2 Likes

i letrally responded because i already did that bro
just actually help me and stop wasting time pleaseeeeee i just wanna get this done man

1 Like

show me your code how it is right now

1 Like

what do you think?

local function VisClear(A)
	local count = #script.Parent.Frame.ScrollingFrame:GetChildren()
	print(count)
	if count >= 30 then
		print("aaaaaa")
		script.Parent.Frame.ScrollingFrame:GetChildren()[2]:Destroy()
	end
end
1 Like

What is the current issue with it now? Your original description was a little confusing. Does everything else work, but changing the count requirement around change which frame gets destroyed?

In that case, just assign the layoutorder or set an attribute to each frame instead of the scrolling frame to a number that represents the order in which they were created. Then, search through the children and look for the frame with that number.

1 Like

so this code should be deleteing the 2nd oldest child (2nd because the first is a list layout, dont want to delete that)
I want to delete the2 oldest child, when i set the number check value to 20 then it works fine and destroys the 2nd oldest child therefore making it look like the text labels i have are flowing.
But if i set the value to anything besides 20 then it destroys the newest child instead of the 2nd oldest one

1 Like

Could you provide a screenshot of the explorer in this situation?

1 Like

For refrence, here is anything besides 20:

And here is 20:

1 Like

There has to be an issue somewhere else in your code. I just can’t see how that script could be the issue…

2 Likes

well idk why it only happens when i change the number… ill look later im busy rn

1 Like

yo if ur still responding i still havnt fixed it yet and ive tried a lot of things
I also tried lowering the nuber and that works fine, its only making the number higher
Have you gotten anymore ideas

1 Like