For Loop Not Looping Correctly

.Greetings! I Recently stumbled upon something whilst making a Cooking System; the for Loop isn’t functioning as intended. It only is looping a part ONCE Out of the multiple times it supposed to. I tried multiple things but couldn’t think of anything since this is my first time experiencing something like this. Here is a little snippet of my code

script.Parent.MouseButton1Click:Connect(function()
	local Cooking = Instance.new("Fire")
	Cooking.Parent = Cooker:FindFirstChild("BaseCooker")
	
	for i, v in pairs(Cooker.BaseCooker:GetChildren()) do
		if v:IsA("BasePart") then

			wait(10)
			v.BrickColor = BrickColor.new("Bright orange")
			wait(2)
			Cooker.BaseCooker:FindFirstChild("Fire"):Destroy()
		end
	end
end)

Any help is appreciated! :smile:

Do you want all the burners to come on at once or one by one?

Preferably at once, after 10 Seconds.

Hi your problem is that you are using a for loop. Which doesn’t loop the kind of function you want. You want a while loop instead.

if I understood your problem correctly

The problem is you’re yielding the current thread of execution in your iterator. To bypass this, create a separate thread of execution so your yields do not yield the iterator, so it runs the code block for each index of the table without yielding at all, like so:

for i, v in pairs(Cooker.BaseCooker:GetChildren()) do
		if v:IsA("BasePart") then
                        coroutine.wrap(function()
			wait(10)
			v.BrickColor = BrickColor.new("Bright orange")
			wait(2)
			Cooker.BaseCooker:FindFirstChild("Fire"):Destroy()
                        end)()
		end
	end
1 Like

I asked a similar question and from my understanding I believe it’s intended that the burners don’t turn on in sync.

Here’s a non co-routine method:

script.Parent.MouseButton1Click:Connect(function()
	local Cooking = Instance.new("Fire")
	local BaseCooker = Cooker:WaitForChild("BaseCooker")
	Cooking.Parent = BaseCooker
	
	wait(10)
	for i, v in pairs(BaseCooker:GetChildren()) do
		if v:IsA("BasePart") then
			v.BrickColor = BrickColor.new("Bright orange")
		end
	end
	wait(2)
	for i, v in pairs(BaseCooker:GetChildren()) do
		if v:IsA("BasePart") then
			Cooker.BaseCooker:FindFirstChild("Fire"):Destroy()
		end
	end
end)
1 Like

this does work, but it will cause a bit of unneccesary processing power, i would just do this:

script.Parent.MouseButton1Click:Connect(function()
	local Cooking = Instance.new("Fire")
	Cooking.Parent = Cooker:FindFirstChild("BaseCooker")
	wait(10)
	for i, v in pairs(Cooker.BaseCooker:GetChildren()) do
		if v:IsA("BasePart") then
			v.BrickColor = BrickColor.new("Bright orange")
		end
	end
	wait(2)
	Cooker.BaseCooker:FindFirstChild("Fire"):Destroy()
end)
3 Likes

Try to multithread your function. The problem is that you have wait() statements so it takes 12 seconds for each part to activate. What I suggest instead is putting a spawn() function when you are doing the effect on each basepart.

Thanks for this, I also tested @C_Sharper’s method, it also works, I agree, it would take unnecessary bits of power, once again, thank you both!