How to get children of an instance in a specific order


So, I have these attachments, and I want to get them in a specific order for a script that tweens a part to each attachment, so i would get the parts in order, but the problem is making it so the attachments are retrieved in a specific order.
Let’s say the part starts at attachment 2, the order would be:
3,4,1,2
or if i start from attachment 3:
4,1,2,3

how could I do this?

add them to a table like this:

local attatchments = {4,1,2,3}

for i,v in pairs(attatchments) do
--code here
end
1 Like

Yeah, but I mean, I want them to be in any order, like I said, so the tween can start from whichever attachment is placed in.

Have the attachaments stored inside a table like @Kaid3n22 said and iterate over the table, if you’re not exactly sure of how to do that you can check out this: Tables | Roblox Creator Documentation

1 Like

But how could i get the values in a specific order for every attachment, I can’t do it manually because in my game this thing has 13 attachments.
The order should be:
The current attachment’s number, then every other attachment with a higher numbe in order from lowest to highest, then every nuber below the current attachment’s number, also ordered from lowest to highest.

local numbers = {3,4,1,2}

for i, v in ipairs(numbers) do
-- code
end

If you name the attachments properly, u could do something like

for i=1,4 do
  Attachment = Model:FindFirstChild("Attachment"..i)
  -- stuff
end

This assumes you’ve named them in the correct order

What im trying to do is to get the attachments in a specific order depending on what attachment the part is currently on. In the example I gave, I want the part to go around every attachment clockwise, so the correct order of attachments changes depending on which one the part started in

u could do something like


local index = 3
local AmountLookedAt = 0
local Max = 4

repeat
  local Attachment = Model:FindFirstChild("Attachment"..index)
  -- do stuff
  AmountLookedAt += 1
  index += 1
  if index>Max then
    index = 1
  end
until AmountLookedAt == Max

result:

2 Likes

From what I understand, you want to tween a part from 1 attachment to the next to make the part follow a path.

You would need to set the attachments in a numerical order to make things easier

Instead of an i,v in ipairs loop, we use a for = 1, #attachments loop to get the numerical order because an i, v in ipairs loop may pick the attachments in the wrong order

local attachmentsFromContainer = attachmentContainer:GetChildren()

for i = 1, #attachmentsFromContainer do
	local thisAttachment = attachmentContainer:FindFirstChild(tostring(i))
	-- this will get the attachments in a numerical order from 1 to the max number of attachment
	-- so you would tween the part to thisAttachment here
end

Result
image


image

Iterating through the array will return every attachement in the order you put them in so you can manipulate them by picking the correct one from the array

1 Like