.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)
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
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)
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)
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.