Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I sort objects that has an ID (1, 2, 3 etc) in order by their ID number?

Asked by 6 years ago

How would I sort a folder that consists of hats, hairs, etc. that has an IntValue to represent which order they should go in inside a table? I'm not sure if I asked this correctly, had to re-type this 4 times. Here's a picture example:

http://prntscr.com/jhgu62

Not sure how to go about this, my brain isn't working entirely well as of now. Could anyone lead me in the right direction?

0
why not make the hat number and the id same? (hat1, id =1) awesomeipod 607 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You can use table.sort to do this, by passing in a comparator function:

-- First create a table of your objects
local objects = workspace.Folder:GetChildren()

-- Now use table.sort to get the order that you want
table.sort(objects, function(obj1, obj2)
    return obj1.ID.Value < obj2.ID.Value
end)

This code assumes your folder is called Folder, and is in the workspace, and the IntValue of each object is called ID. You will need to change this to match how you have things set up.

Ad

Answer this question