How do I get parts in order depending on direction?

here is the deal

I have a model with red parts inside workspace, I want to change the color of the parts in order from bottom to top

image

Using i, v in pairs doesn’t really achieve that as they’re selected randomly.

Things to keep in mind:

  • The model’s rotation might not always be the same

  • In the future, there will be way too many parts to change their color one by ones

The easiest way to do this would be to just keep track of the order when the parts are generated. If you’re making these parts manually, try numbering them with IntValues.
If you’re creating them with scripts, add them to a table and loop over the table when you want to change their colours.

They’re made manually but I used Clone alot, like alot, there’s easily 100+ parts

Are you changing the parts’ colours in pairs from top to bottom like this?

If so, there’s one solution I can think of. If you’re changing them one by one though, it’d require a pretty complex solution and you’d be better off just labelling each part with a number.

Yes I’m doing it in pairs, but keep in mind, for i, v in pairs doesn’t do it from top to bottom, it does it randomly.

Edit: Also I’m pretty sure switching from pairs to ipairs won’t fix it.

Ok what you could do is first group the parts in pairs (perhaps as a model).
Then add an invisible part off in the distance (pictured below as the green circle).
Then, get the distance from that part to each of the model pairs. Add them to a table ordered by distance.
Then loop over that table.

I can write up an example if you’d lke

Yes please write an example before my brain catches on fire

You could use table.sort and sort it by order from position, top to bottom or bottom up. Then you could use the pairs way your using?

Explorer:

Script:

local function getOrdered(model)
	local t = {}
	for i,v in pairs(model:GetChildren()) do
		if not v:IsA('Model') then continue end
		local contents = v:GetChildren()
		local midPoint = (contents[1].Position + contents[2].Position)/2
		local distance = (midPoint - model.StartPart.Position).Magnitude
		table.insert(t, {model = v, dist = distance})
	end
	
	table.sort(t, function(a,b)
		return a.dist < b.dist
	end)
	
	return t
end

while true do
	task.wait(0.5)
	for i,v in pairs(getOrdered(workspace.Parts)) do
		for _,j in pairs(v.model:GetChildren()) do
			j.Color = Color3.new(1,0,0)
			game.TweenService:Create(j, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {Color = Color3.new(1,1,1)}):Play()
		end
		task.wait(0.05)
	end
end

The function returns nil for me, when I print the part, it prints the part name, but when i loop through the “ordered” table to print every parts name, it just prints “nil” like alot of times

Can you screenshot the code and also the explorer, showing how your parts are grouped?

I was about to send you a long long message with screenshots and stuff but I noticed the reason it wasn’t working is because I typed t.Part with an uppercase P instead of t.part

:skull:

Thanks man, today I learnt something new, table.sort.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.