Code Issues - For Loop textLabel not updating correctly

Hello! I would like to make my textLabel update from 5 to 0 simultaneously while the loading bars are increasing in ascending order. How can I achieve this?

What I need is to make the number “1” that I have marked in the image go from 5 to 0, so it would be “5, 4, 3, 2, 1,” while the for loop for the loading bars is increasing in ascending order. The way it is currently working for the loading bars is perfect. How can I achieve this with the textLabel marked in the image so that it goes from 5 to 0?

localscript:

-- Countdown timer loop
		for i = countdownDuration, 0, -1 do
			if shouldContinueCountdown then
				
				-- Change the color of each bar in loading_Cooking_UI
				local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

				if loadingGui then
					local allBarsGreen = true  -- Flag to check if all bars are green
					
					local vars = countdownDuration - i + 1

					for _, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
						if bar:IsA("Frame") then
							local textLabelCounter = loadingGui.Frame.counterTextLabel
							-- Assuming you have white bars, you can change the color to something else
							bar.BackgroundColor3 = Color3.new(0, 1, 0)  -- Change to green (adjust as needed)

							if bar.BackgroundColor3 ~= Color3.new(0, 1, 0) then
								allBarsGreen = false  -- If any bar is not green, set the flag to false
							end
							textLabelCounter.Text = "The cooking is " .. vars .. " seconds left until finished."
							wait(1)
						end
					end

					if allBarsGreen then
						break  -- Exit the loop if all bars are green
					end
				else
					warn("loading_Cooking_UI not found.")
				end

				wait(1)
			else
				break  -- Exit the loop if shouldContinueCountdown is false
			end
		end
3 Likes

Look into coroutines for multi threading

1 Like

I tried but it doesn’t work :frowning:

the text stays at 5 and dont decreases

-- Countdown timer loop
		for i = countdownDuration, 0, -1 do
			if shouldContinueCountdown then
				-- Change the color of each bar in loading_Cooking_UI
				local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

				if loadingGui then
					local allBarsGreen = true  -- Flag to check if all bars are green

					for _, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
						if bar:IsA("Frame") then
							local textLabelCounter = loadingGui.Frame.counterTextLabel
							-- Assuming you have white bars, you can change the color to something else
							bar.BackgroundColor3 = Color3.new(0, 1, 0)  -- Change to green (adjust as needed)

							if bar.BackgroundColor3 ~= Color3.new(0, 1, 0) then
								allBarsGreen = false  -- If any bar is not green, set the flag to false
							end

							-- Start a coroutine for updating the text label
							local updateTextCoroutine = coroutine.create(function()
								for j = 5, 1, -1 do
									textLabelCounter.Text = "The cooking is " .. j .. " seconds left until finished."
									wait(1)
								end
							end)
							coroutine.resume(updateTextCoroutine)  -- Start the coroutine

							wait(1)
						end
					end

					if allBarsGreen then
						break  -- Exit the loop if all bars are green
					end
				else
					warn("loading_Cooking_UI not found.")
				end

				wait(1)
			else
				break  -- Exit the loop if shouldContinueCountdown is false
			end
		end
1 Like
local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

local textLabelCounter = loadingGui.Frame.counterTextLabel

local bars = {} do 
    -- By the way, I truly don't recommend this approach (using GetChildren alone).
    -- The order of the bars inside the hierarchy is unknown.
    -- Good code should have a way to check which bar comes after the other, such as by postfixing
    -- them with an index. Then, we'd sort the `bars` table according to that index.
    for _, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
        if bar:IsA("Frame") then
            table.insert(bars, bar)
        end
    end
end

textLabelCounter.Text = "The cooking is 5 seconds left until finished."

for index, bar in ipairs(bars) do
    task.wait(1)

    textLabelCounter.Text = "The cooking is " .. #bars - i .. " seconds left until finished."

    bar.BackgroundColor3 = Color3.new(0, 1, 0)
end
1 Like

You can use ipairs command to fill each squares green.

1 Like

I tried it and it didn’t work :confused:

1 Like

Rename the bars to 1, 2, …, 5

local time_ = 5

for i = time_, 0, -1 do -- for loops use >= comparison so you have to lower the iterations by 1 (5 - 1)
    textLabel.Text = `The cooking is {i + 1} seconds left until finished.` -- i + 1 to recover the 1 subtracted earlier

    local bar = loadingGui.Frame.loading_bar:FindFirstChild(tostring(-i + time_)) -- Get the reverse of i, so that if i = 4 the bar index will be 1
end
1 Like

i tried it but still dont work :frowning:

-- Countdown timer loop
		local time_ = countdownDuration

		for i = time_, 1, -1 do
			if shouldContinueCountdown then
				-- Change the color of each bar in loading_Cooking_UI
				local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

				if loadingGui then
					local allBarsGreen = true  -- Flag to check if all bars are green

					for _, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
						if bar:IsA("Frame") then
							local textLabelCounter = loadingGui.Frame.counterTextLabel
							bar.BackgroundColor3 = Color3.new(0, 1, 0)  -- Change to green (adjust as needed)

							if bar.BackgroundColor3 ~= Color3.new(0, 1, 0) then
								allBarsGreen = false  -- If any bar is not green, set the flag to false
							end

							textLabelCounter.Text = "The cooking is " .. i .. " seconds left until finished." -- Display the countdown from 5 to 1
							wait(1)
						end
					end

					if allBarsGreen then
						break  -- Exit the loop if all bars are green
					end
				else
					warn("loading_Cooking_UI not found.")
				end

				wait(1)
			else
				break  -- Exit the loop if shouldContinueCountdown is false
			end
		end

Never mind! I managed to do it, lol! Oh my God… it’s almost 2 am in my country, and I was able to figure it out. Thank you all for your help, and good night!

-- Countdown timer loop
		for i = countdownDuration, 0, -1 do
			if shouldContinueCountdown then
				local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

				if loadingGui then
					local allBarsGreen = true  -- Flag to check if all bars are green

					for x, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
						if bar:IsA("Frame") then
							local textLabelCounter = loadingGui.Frame.counterTextLabel
							bar.BackgroundColor3 = Color3.new(0, 1, 0)  -- Change to green (adjust as needed)

							local vars = countdownDuration - x + 1
							textLabelCounter.Text = "The cooking is " .. vars .. " seconds left until finished."

							wait(1)
						end
					end

					if allBarsGreen then
						break  -- Exit the loop if all bars are green
					end
				else
					warn("loading_Cooking_UI not found.")
				end

				wait(1)
			else
				break  -- Exit the loop if shouldContinueCountdown is false
			end
		end
1 Like

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