Countdown Loop Issue: Player Movement and GUI Hide Problem

I’m facing an issue with my countdown loop – after it turns ‘loading_bar’ elements green, the player can’t move, and the GUI doesn’t hide. Seeking guidance on the problem and a fix. Any insights or solutions appreciated!

image

video showing error:

localscript:

-- Function to show the frame and hide it after a specified time
local function showAndHideFrame(player)
	print("Showing the frame...")
	frameToShowHide.Visible = true  -- Show the frame

	-- Load and play the sound during the countdown
	loadSound()
	playCountdownSound()
	controlPlayerMovement(false, 8)

	-- Getting the local player
	local player = game.Players.LocalPlayer

	-- Getting the PlayerGui
	local playerGui = player:FindFirstChild("PlayerGui")

	-- Verify that the PlayerGui exists
	if playerGui then
		-- Now show the new GUI
		local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

		if loadingGui then
			loadingGui.Enabled = true
			print("loading_Cooking_UI shown.")
		else
			warn("loading_Cooking_UI not found.")
		end

		-- Countdown timer loop
		for i = countdownDuration, 0, -1 do
			if shouldContinueCountdown then
				loadingTextButton.Text = "The cooking is " .. i .. " seconds left until finished."

				-- Change the color of each bar in loading_Cooking_UI
				local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

				if loadingGui then
					for barIndex, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
						if bar:IsA("Frame") then
							-- 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)
							wait(1)
						end
					end
				else
					warn("loading_Cooking_UI not found.")
				end
			else
				break  -- Exit the loop if shouldContinueCountdown is false
			end
		end

		-- Wait for 1 second after the loop
		wait(1)

		-- Now hide the loading_Cooking_UI after the countdown
		local loadingGui = playerGui and playerGui:FindFirstChild("loading_Cooking_UI")

		if loadingGui then
			loadingGui.Enabled = false
			print("loading_Cooking_UI hidden.")
		else
			warn("loading_Cooking_UI not found.")
		end


		controlPlayerMovement(true, 16)

		print("Hiding the frame...")
		frameToShowHide.Visible = false  -- Hide the frame after the specified time

		local player = game.Players.LocalPlayer

		if player then
			-- Check if cancel button is pressed
			if isCancelButtonPressed then
				print("Cancel button pressed. Tool not added to the player's backpack.")
				controlPlayerMovement(true, 16)
			else
				-- Add the specific tool to the player's backpack
				if specificTool then
					print("Adding the specific tool to the player's backpack...")
					local toolClone = specificTool:Clone()
					toolClone.Parent = player.Backpack
				else
					warn("Specific tool not found.")
				end

				-- Reset the cancel button state and the countdown status
				isCancelButtonPressed = false
				shouldContinueCountdown = true
			end
		else
			warn("Player not found.")
		end
	end
end
1 Like

Forget it, I already managed to solve it. Lol. :smile_cat:

-- Countdown timer loop
		for i = countdownDuration, 0, -1 do
			if shouldContinueCountdown then
				loadingTextButton.Text = "The cooking is " .. i .. " seconds left until finished."

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

		-- Reset the color of each bar to gray
		local loadingGui = playerGui:FindFirstChild("loading_Cooking_UI")

		if loadingGui then
			for _, bar in ipairs(loadingGui.Frame.loading_bar:GetChildren()) do
				if bar:IsA("Frame") then
					-- Assuming you have white bars, you can change the color to something else
					bar.BackgroundColor3 = Color3.new(0.862745, 0.862745, 0.862745)  -- Change to gray (adjust as needed)
				end
			end
		else
			warn("loading_Cooking_UI not found.")
		end
1 Like

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