So I have a Frame / Scrolling Frame, how to make it removes child not based on name, but based on the first child (the first child that comes inside this Frame / ScrollFrame)
Maybe use Destroy() to destroy any instances Instance:Destroy
You can use ChildAdded
event, and when it happened, remove that child by using Destroy()
.
Example:
--on 1 script
local Frame = script.Parent.Frame
Frame.ChildAdded:Connect(function(child)
task.wait(2)
child:Destroy()
end)
--on 2 script
local Frame = script.Parent.Frame
task.wait(2)
local button = Instance.new("TextButton")
button.Parent = Frame
Yes, but let’s say I fire or put too many child inside it so fast I want if it already reach five children, it’s gonna remove the first child.
--on 1 script
local Frame = script.Parent.Frame
Frame.ChildAdded:Connect(function(child)
task.wait(2)
child:Destroy() -- it'll remove all the children that were added
end)
--on 2 script
local Frame = script.Parent.Frame
task.wait(2)
while task.wait(2.1) do
local button = Instance.new("TextButton")
button.Parent = Frame
end
If you want to only remove the first one,
you’ll have to add an if-statement, that would check if the amount of children inside the Frame is 1 - and that’ll simply vanish it.
Or, you could also check if that child was added first [ you could name those instances for e.g - in numbers, and you can check accordingly to that, which was added first and remove it].
Another possible way, is to store them in a table, and check [with their indexes], who was first.
Maybe store the children in a table so you can find the first array. Then compare the array with the first child and if they are the same name, destroy that child. Then empty the table and record the children again. Tables
This makes me think you’re doing something wrong. Can’t you just not make the certain “first child” a member of the Frame’s children?
Otherwise @Valkyrop 's method using the ChildAdded
event is the right approach (although not sure about this new task.wait
function and the part involving it since I’ve been off for a long while, but it’s probably alright).
No, because in my thinking is like this. Let’s say I have a child let’s say a frame inside scroll frame and it lasts for 8 seconds before it get destroyed (normally). But before that 8 seconds, I make 5 another frames so it’s gonna be 6 right? Because it’s 6, I want it to destroy the first child (because I want the maximum to be just 5 in the scrolling frame)
Before adding the next frame, you can check whether you should based on how many frames currently exist. For instance:
if #FrameParent:GetChildren() < 5
-- we can add a frame now
end
this way you’re only adding another frame when you’re sure adding another frame would not make the total frames exceed 5, instead of continuously adding frames and removing excess ones.
What script are you using to add the children? @robloxjw02
I have a potential method to remove the first instance, but I need to modify the script that puts the parts there
Well yes, ah but can we make like this, because I supposed this is for group game so not everyone know only below 5, so when they want to give/fire frame if currently is more than 5, it will wait until below 5 then it will automatically fire frame?
i use local then fire to server than to all client.
I meant the code of the script that puts the instances in the Frame