Changing index of a hierarchy with a script?

For a replacing feature to work, I need to be able to change the index of a child. For example, let’s say the hierarchy looks like this:

Folder

  • Value1
  • Value2
  • Value3

I would like to be able to do something like this:

changeIndex(Value3,2)

Changing it to:

Folder

  • Value1
  • Value3
  • Value2

I have tried doing Instance:GetChildren()[2] = child, and also have tried table.insert. I can’t find a way to do this, so if you know how, please tell me! Thanks for reading.

Test code:

local new = Instance.new("StringValue",workspace.StringValues) new.Name = "NewValue" workspace.StringValues:GetChildren()[2] = new
print(workspace.StringValues:GetChildren()[2].Name)

I might be able to do this with table.sort, but am not sure how I would make the index take priority only in the specific position, while still keeping position of the others.

I did manage to do this, by making a script remove the other children in the folder, parenting them to another, then inserting the ones I needed, and re-adding the ones after back to it. Not the most efficient way, but it works.