I have multiple objects (hundreds), and I want to rename them in order (1,2,3…) based on it’s y value.
So top or bottom is 1, and the number gets bigger as it goes up/ down. How would I do that?
for i,v in pairs(parts) do
v.Name = v.Position.Y
end
Seriously…
well I know about that, but I said in order like 1,2,3 and the parts are in all kinds of y axis’, so it would probably look like 1,7,19,21,16
You could put all of the objects’ y-values into an array, then sort them by their y-values. The indices of the array should then match up with the magnitude of the y-values with respect to each other. To sort the array, you could either use your own algorithm or the basic table.sort() method.
Try this:
local lastPart = 0
for _, part in pairs(parts) do
lastPart = lastPart + 1
part.Name = tostring(lastPart)
end
If you have the parts in an array, you can sort them by height with table.sort
local parts = {}
table.sort(parts, function(a, b)
return a.Position.Y < b.Position.Y
end)
-- now that parts are ordered by height, name them based on their index in the table
for index, part in pairs(parts) do
part.Name = index
end
Then you would use table.sort()
Here’s some code I have some code I made for you.
local parts = workspace.Parts:GetChildren() -- This would be folder full of parts you want to rename
local tableToSort = {}
for i,v in pairs(parts) do
table.insert(tableToSort,{v,v.Position.Y})
end
table.sort(tableToSort,
function(a,b)
return a[2] < b[2]
end
)
for i,v in pairs(tableToSort) do
if type(v) == "table" then
v[1].Name = v[2]
end
end
This would order them from least to greatest.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.