Loop with multiple parts

I am trying to make this here function with more than the single part it is functioning with right now. I know that the while wait loop will cause it to only work for one part, so what is a way I can try to make it work for all the hotParts?

for _, hotParts in pairs(CS:GetTagged("HotPart")) do
	local killTimer = 3

	while task.wait(killTimer) do
		print(hotParts.BrickColor, hotParts.Name)

		if hotParts.BrickColor == BrickColor.new("CGA brown") then
			print("hot yes")
			hotParts.BrickColor = BrickColor.new("Medium stone grey")

		elseif hotParts.BrickColor == BrickColor.new("Medium stone grey") then
			print("yes cold")
			hotParts.BrickColor = BrickColor.new("CGA brown")
		end
	end
end

I have tried making it it’s own function but didn’t change anything, i feel as though I am forgetting something simple to make it work, can anyone please help me?

I’d appreciate it ! :smiley:

1 Like
local CS = game:GetService("CollectionService")
local killTimer = 3

while true do
    -- wait
	task.wait(killTimer)
	-- change part color
	for _, hotPart in pairs(CS:GetTagged("HotPart")) do
		if hotPart.BrickColor == BrickColor.new("CGA brown") then
			print("hot!")
			hotPart.BrickColor = BrickColor.new("Medium stone grey")
		elseif hotPart.BrickColor == BrickColor.new("Medium stone grey") then
			print("cold!")
			hotPart.BrickColor = BrickColor.new("CGA brown")
		end
	end
    -- repeat
end

Instead of putting multiple while loops inside of the for loop, it is much simpler and more efficient to put the for loop inside of a single while loop. The code above should solve your problem.