Changing parts color in order

I am trying to change the parts brickcolor in order by looping through the children. I have 1 problem, why does the output print “ran first” last and “ran 2nd” first? Code:

local Model = workspace.Model

--k = 1, 2, 3, 4
--#children = 4

--v = 2, 3, 4, 1

--k - v.Value = 1

--1, 2, 3, 4
--4, 3, 2, 1


fi = 0 -- first index [fi = k]

for k, v in ipairs(Model:GetChildren()) do
	if Model[v.Name].pos.Value > k then
		test = Model[v.Name].pos.Value / k * k
		if math.abs(tonumber(k) - Model[v.Name].pos.Value) < k then
			local add = Model[v.Name].pos.Value + tonumber(k) - k --3, 4
			if Model[v.Name].pos.Value < #Model:GetChildren()  then
				Model[v.Name].BrickColor = BrickColor.Black()
				print('3rd')
			else
				Model[v.Name].BrickColor = BrickColor.Black()
				print('4th')
			end
		
		else
			--2nd
			Model[v.Name].BrickColor = BrickColor.Black()
			print("ran 2nd")
		end
		
	else
		--1st
		Model[v.Name].BrickColor = BrickColor.Black()
		print("ran first")
	end
	
end

It looks like you’re making all of the colors black? what is the point if you are doing that

1 Like

Well anyways I guess what you could do is loop through the amount of parts there are like this and get each pos value

local model = workspace.Model

local function changeColor(pos)
  for k,v in pairs(model:GetChildren()) do
    if (v.pos.Value == pos) then
      v.BrickColor = BrickColor.Black()
      print("ran "..pos)
    end
  end
end

for i = 1, #model:GetChildren() do
  changeColor(i)
end

if this fixes your problem please consider setting it as the solution! :smiley:

Just to see the parts change in color chronologically

Did the solution i gave work? if it did please set as the solution!!!