Countdown isn't working properly

  1. What do you want to achieve?
    I want a countdown of 120 seconds to play during the game and then have the text change to “Times up!”

Kinda like when a player has a timed obby and only x amount of time to complete the obby, then the game will end. In this game, the player has x amount of time to complete as many orders as they can before the timer runs out.

  1. What is the issue?
    The countdown plays separately from the game. It will first countdown and then the rest of the game will run.

Here’s a video of the issue:

P.S. Ignore the all the random output stuff, it’s just other things in the game that I forgot to remove before recording the video! But you’ll see that it prints the second it’s on.

  1. What solutions have you tried so far?
    I’ve used ChatGPT to help me figure out what I’m doing wrong but it hasn’t been very useful at all as it gives me different answers, writes completely unrelated code/writes in a different scripting language.

Workspace Set-up
Screen Shot 2023-01-27 at 1.31.01 PM
The other Localscripts trigger the ‘Job’ and ‘JobEnd’ RemoteEvents (I haven’t added the ‘JobEnd’ function into the Main script yet though.

Main Script:
I also used 10 seconds instead of 120 so I could test whether the countdown worked

local jobGui = script.Parent
local countdowntext = jobGui.Countdown

local characters = {"Hello Kitty"} -- temporary
while(1) do
	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()
	
	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace

--Countdown
	countdown = 10 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
	end
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0

	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()
	
	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end
			
		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	while not ProximityPromptTriggered do
		wait()
	end
	
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	anim:Play()
	clone.Humanoid:MoveTo(workspace.start.Position)
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 
	
	end
	clone:Destroy()
	wait(2)
end

ServerScriptService Script:
This script is for the initial 5 second countdown!

game.Players.PlayerAdded:Connect(function(player)
	local countdown = 5
	local jobGui = player.PlayerGui
	local countdowntext = jobGui:WaitForChild("Job").Countdown
	countdowntext.Visible = true
	countdowntext.Text = "Starting game in: " .. countdown
	print("Starting game in: " .. countdown)
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = "Starting game in: " .. countdown
		wait(1)
		print(countdown)
	end
	countdowntext.Text = "GO!"
	print("GO!")
	wait(.5)
	game.ReplicatedStorage.Job:FireClient(player)
end)

Why did you use ChatGPT in the first place?!?! That’s on you to debug it because it was NOT made by a human.

1 Like

I didn’t use ChatGPT to create/write the script, in fact, I wrote the script myself. I was using it as a tool to help me figure out what I was doing wrong but it was not helpful.

This is in Main Script.
So my question is why are you making the countdowntext visible after the countdown is finished?




I think you need to place this event connection outside of the while loop. I think.
To do that you need to change a little bit of the inside of the code inside and outside the while loop.

Beginning of script:

local clone = nil -- before while loop and event connection
local countdownFinished = false
local ProximityPromptTriggered = false

inside the event:

if not clone then return end -- if clone is not created yet then exit the function
if countdownFinished == false then return end -- if countdown is still going then exit the function

inside the while loop:

-- instead of local clone, just overwrite the current clone
clone = characterToClone:Clone()
countdownFinished = false
ProximityPromptTriggered = false
...
print("STOP!")
countdownFinished = true
1 Like

Hi. There’s nothing wrong with using ChatGPT. It’s a powerful tool that can offer you help and explain things for you, especially for code. However, make sure you double-check the code it gives you and make sure you understand how it works. I think that the main issue is the flow of your script and the order of changes being made. For example, @weakroblox35 bought up a good point, the line:

countdowntext.Visible = true

-should have been been placed before the countdown started.

Here’s an improved version of your MainScript:

local Players = game:GetService("Players")

local jobGui = script.Parent
local countdowntext = jobGui.Countdown
local characters = {"Hello Kitty"} -- temporary

-- Starts a countdown for all the players from the specified duration
function startCountdownForAllPlayers(countdown_Duration:number)
	for _, player:Player in pairs(Players:GetChildren()) do
		task.spawn(function()
			local jobGui = player.PlayerGui
			local countdowntext = jobGui:WaitForChild("Job").Countdown

			countdowntext.Visible = true
			countdowntext.Text = "Starting game in: " .. countdown_Duration
			print("Starting game in: " .. countdown_Duration)

			for Seconds_Left = countdown_Duration, 0, -1 do
				countdowntext.Text = "Starting game in: " .. Seconds_Left
				print(Seconds_Left)
				wait(1)
			end

			countdowntext.Text = "GO!"
			print("GO!")
			wait(0.5)
			game.ReplicatedStorage.Job:FireClient(player)
		end)
	end 
end

while wait(1) do
	local TextButton = script.Parent.Frame.TextButton
	TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()
	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')

	clone.Parent = workspace
	countdowntext.Visible = true
	--Countdown
	local countdown_Duration = 10 --120
	-- Starts countdown for players
	startCountdownForAllPlayers(countdown_Duration)
	-- An alternate/better loop for a countdown
	for Seconds_Left = countdown_Duration, 0, -1 do 
		countdowntext.Text = Seconds_Left
		print(Seconds_Left)
		wait(1)
	end
	print("STOP!")
	
	countdowntext.Text = "Times up!"
	
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0

	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()

	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end

		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	-- Waits until ProximityPromptTriggered is true
	repeat task.wait() until ProximityPromptTriggered == true

	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	clone.Humanoid:MoveTo(workspace.start.Position)
	anim:Play()
	
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 
	end
	
	clone:Destroy()
	wait(2)
end

No need to use the other script in ServerScriptService. Let me know if it works or if you have any questions. Good luck!

2 Likes

Thank you so much for your reply! It was extremely helpful and kind :slight_smile: :sparkling_heart:

First off, you’re completely right about ChatGPT — it’s a useful tool to use when you get stuck on something; however, it’s important to have a somewhat good understanding of scripting in Lua before using it for everything, as indeed it is not always correct. This is why I seldom try to use it as a tool instead of just blindly using it to create/write a script!

Secondly, I sincerely thank you for taking the time to add some improvements to my Main script! I can definitely see how the flow of my original script was not very good. I’m quite new to scripting and still continue to learn each time I work on something new and I’m very grateful for all of the geniuses on the Roblox DevForum.

Unfortunately, I’m having a few issues with the script you’ve provided… :sweat_smile:

Since the MainScript is a local script within the Job GUI, the game.ReplicatedStorage.Job:FireClient(player) for the “starting game in…” countdown does not fire and I get the error, FireClient can only be called from the server however, it doesn’t make much of a difference whether I keep or remove this line from the script if I have a ServerScript in ServerScriptService saying

game.Players.PlayerAdded:Connect(function(player)
	game.ReplicatedStorage.Job:FireClient(player)
end)

Without the ServiceScriptService Script, the game won’t start since the Job remote event (RE) won’t fire.

In one of the Localscripts within the Job GUI, once the Job RE fires, it enables the “Main” script which then starts the game.

game.ReplicatedStorage.Job.OnClientEvent:Connect(function()

	if script.Parent.active.Value == 5 then
	script.Parent.Frame.Visible = true
	script.Parent.Main.Disabled = false
	script.Parent.active.Value = 0
	end
end)

The “active” NumberValue determines whether the game ends or starts, so if the Job RE is fired and the active.value = 5 then it will change the active.value = 0 and start the game by enabling the “Main” script. Then, once “Main” script is enabled, I want the 120 second countdown to start. This is also why I had my original ServerScriptService script do the “starting game…” countdown as then it’d complete that 5-second countdown, and fire the Job RE which will start the game and the 120 second countdown.

Another issue I’m having with the script you’ve given to me is that the clone spawns, 120 second countdown will play, and then everything else happens how I wanted it to, but then the 120 second countdown restarts and the new clone pauses for the countdown before walking to the counter.

What I intend to happen is the countdown will start and the customer/character will clone, walk to the counter (basically do everything I’d like it to) and this part will repeat until the 120 second countdown is at 0. Then once the 120 second countdown is at zero, the game will end and everything will stop.

After the game ends, I was planning on probably adding a line saying game.ReplicatedStrorage.JobEnd:FireServer(player) which basically disables everything that the game.ReplicatedStrorage.Job:FireClient(player) did — the JobEnd will be a RE in ReplicatedStorage. This will also bring up a FinalScore GUI, etc., and a ‘Leave’ button to exit the game.

Kinda like when a player has a timed obby and only x amount of time to complete the obby, then the game will end. In this game, the player has x amount of time to complete as many orders as they can before the timer runs out.

Overall, however, I really do appreciate your help and I hope that maybe you’ll be able to help me more with what I’m trying to accomplish in regards to the 120 countdown. :slight_smile:

1 Like

Hi there!

Firstly, thank you so much for your response — I am very grateful to have your help. :slight_smile:

Regarding the countdowntext.Visible = true when messing around with my scripts to hopefully fix my issue, I accidentally put it into the wrong place.

I can kind of see what you’re explaining to me and how I can go about fixing my issue — I’m quite new to scripting and have only been doing it for a few months so I appreciate your help immensely.

I broke the script down to the parts I’ve added to my script using your comments! Let me know if I’m just confused about your reply and just not doing it right.
EDIT: Currently, I don’t think I did it right cause I’m continuing to have the same issue & now more issues…

Please let me know if I’m mistaken or confused about your reply, but from what I understand, these are the changes I should make to the Main script within the Job GUI:

Beginning of script:

local clone = nil -- before while loop and event connection
local countdownFinished = false
local ProximityPromptTriggered = false
local jobGui = script.Parent
local countdowntext = jobGui.Countdown

--local characters = {"Kuromi", "Keroppi", "Hello Kitty", "Cinnamoroll", "My Melody", "Pompompurin"}
local characters = {"Hello Kitty"} -- temporary
while(1) do
	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()
	
	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace
	
	countdown = 10 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
	end
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0

Inside the event:

	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	
	
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if not clone then return end -- if clone is not created yet then exit the function
		if countdownFinished == false then return end -- if countdown is still going then exit the function
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end
			
		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)

Inside the while loop:

	while not ProximityPromptTriggered do
		wait()
		clone = characterToClone:Clone()
		countdownFinished = false
		ProximityPromptTriggered = false
	end
	
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end

Here’s the entire script if you’d rather look at that instead!

Entire Script
local clone = nil -- before while loop and event connection
local countdownFinished = false
local ProximityPromptTriggered = false
local jobGui = script.Parent
local countdowntext = jobGui.Countdown

--local characters = {"Kuromi", "Keroppi", "Hello Kitty", "Cinnamoroll", "My Melody", "Pompompurin"}
local characters = {"Hello Kitty"} -- temporary
while(1) do
	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()
	
	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace
	
	countdown = 10 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
	end
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0

	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()
	
	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	
	
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if not clone then return end -- if clone is not created yet then exit the function
		if countdownFinished == false then return end -- if countdown is still going then exit the function
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end
			
		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	while not ProximityPromptTriggered do
		wait()
		clone = characterToClone:Clone()
		countdownFinished = false
		ProximityPromptTriggered = false
	end
	
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	anim:Play()
	clone.Humanoid:MoveTo(workspace.start.Position)
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 
	
	end
	clone:Destroy()
	wait(2)
end
	

EDIT: I’ve revised the script and I think I’ve placed everything into the correct location now. Here’s a video of how it’s working: countdownissues2 on Vimeo
However, as I indicated in my reply to @davie10863, having the countdown in-between each cloning of the character/customer is not what I’m trying to accomplish.

Kinda like when a player has a timed obby and only x amount of time to complete the obby, then the game will end. In this game, the player has x amount of time to complete as many orders as they can before the timer runs out.

Overall, however, I really do appreciate your help and I hope you can continue to help me fix this issue :sparkling_heart:

I need to apologize because I didn’t read the original post correctly.
Please try this code modified from the original script:

local jobGui = script.Parent
local countdowntext = jobGui.Countdown
local startstop = Instance.new("BindableEvent")
local started = false

coroutine.wrap(function()
while wait(3) do
 startstop:Fire(true)
 repeat wait() until started == true
 repeat wait() until started == false
end
end)()

startstop.Event:Connect(function(start)
	if start==true and started==false then
		started = true
	elseif start==false and started==true then
		started = false -- force stop from event fire
		return -- exit function
	else
		return -- exit function
	end
	--Countdown
	countdown = 10 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
		if not started then -- force stop
			countdown = 0
		end
	end
	started = false
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0
end)

local characters = {"Hello Kitty"} -- temporary
while true do
	while started==false do
		startstop.Event:Wait()
	end
	
	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()
	
	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace

	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()
	
	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end
			
		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	while not ProximityPromptTriggered do
		wait()
	end
	
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	anim:Play()
	clone.Humanoid:MoveTo(workspace.start.Position)
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 
	
	end
	clone:Destroy()
	wait(2)
end

The issue you explained is because it’s doing the entire countdown, and then starting the actual game.
But what you want, by my understanding, is to start the countdown and then run the game until the countdown ends. So what I did was put the countdown into a seperate task so the game is not waiting on the countdown.

1 Like

Thank you so much, and it’s no worries at all! :slight_smile:

I tried using this script you provided and the countdown will run but the clone.Parent = Workspace line won’t run (or anything after that to my knowledge). So when the countdown is running, the game is running but it’s not cloning the character/customer.

are you doing a print to check it because by the looks of it there is no way to know that without a print or something.

Also I edited it and added in something but Idk if you added it.
it was this code:

1 Like

I added that part into the code and added a print here:

local characters = {"Hello Kitty"} -- temporary
while true do
	while started==false do
		startstop.Event:Wait()
	end

	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()

	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')

	clone.Parent = workspace
	print("Clone created!")
	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()

The print("Clone created!") is not printed in the output.


EDIT: I fixed the script so that it spawns the clone, while started == false do was supposed to be true. Then I added started = false after the clone:Destroy() so that another clone would spawn after the 1st clone is destroyed, however now it ends the countdown when the 1st clone is destroyed.

local jobGui = script.Parent
local countdowntext = jobGui.Countdown
local startstop = Instance.new("BindableEvent")
local started = false

coroutine.wrap(function()
	while wait(3) do
		startstop:Fire(true)
		repeat wait() until started == true
		repeat wait() until started == false
	end
end)()

startstop.Event:Connect(function(start)
	if start==true and started==false then
		started = true
	elseif start==false and started==true then
		started = false -- force stop from event fire
		return -- exit function
	else
		return -- exit function
	end
	--Countdown
countdown = 20 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
		if not started then -- force stop
			countdown = 0
		end
	end
	started = false
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0
end)

local characters = {"Hello Kitty"} -- temporary
while true do
	while started==true do --false
		startstop.Event:Wait()
	end

	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()

	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace
	print("Clone created!")
	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()

	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end

		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	while not ProximityPromptTriggered do
		wait()
	end

	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	anim:Play()
	clone.Humanoid:MoveTo(workspace.start.Position)
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 

		end
	
	clone:Destroy()

	started = false
	
	wait(2)
	
	end

It’s supposed to be while started==false do
However when startstop event is fired, started is changed to true a little bit too late and so it yields again.

1 Like

Try this

local jobGui = script.Parent
local countdowntext = jobGui.Countdown
local startstop = Instance.new("BindableEvent")
local started = false

coroutine.wrap(function()
	while wait(3) do
		started = true
		startstop:Fire()
		repeat wait() until started == true
		repeat wait() until started == false
	end
end)()

startstop.Event:Connect(function()
	--Countdown
	countdown = 20 --120
	while countdown > 0 do
		countdown = countdown - 1
		countdowntext.Text = countdown
		wait(1)
		print(countdown)
		if not started then -- force stop
			countdown = 0
		end
	end
	started = false
	print("STOP!")
	countdowntext.Visible = true
	countdowntext.Text = "Times up!"
	wait(1)
	countdowntext.Visible = false
	script.Parent.active.Value = 0
end)

local characters = {"Hello Kitty"} -- temporary
while true do
	while started==false do
		startstop.Event:Wait()
	end

	script.Parent.Frame.TextButton.LocalScript.Disabled = true
	wait(2)
	local randomIndex = math.random(1, #characters)
	local characterToClone = game.ReplicatedStorage.NPCs[characters[randomIndex]]
	local clone = characterToClone:Clone()

	local animation = clone:WaitForChild('Animation')
	local humanoid = clone:WaitForChild('Humanoid')
	
	clone.Parent = workspace
	print("Clone created!")
	local anim = humanoid:LoadAnimation(animation)
	anim:Play()
	clone.Humanoid:MoveTo(workspace.endo.Position)
	wait(1)
	anim:Stop()

	local ma = math.random(1,4)
	if ma == 1 then
		clone.Head["1"].Enabled = true
	elseif ma == 2 then
		clone.Head["2"].Enabled = true
	elseif ma == 3 then
		clone.Head["3"].Enabled = true
	elseif ma == 4 then
		clone.Head["4"].Enabled = true
	end
	local ProximityPromptTriggered = false
	-- Wait until player triggers proximity prompt
	clone.Head.ProximityPrompt.Triggered:Connect(function()
		if clone.Head["1"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("coffee") then --these are swapped btw, 1 = coffee but the tool = donut, it works tho!
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["1"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["1"].Enabled = true
			end

		elseif clone.Head["2"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("doughnut") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["2"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["2"].Enabled = true
			end
		elseif clone.Head["3"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("green tea") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["3"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["3"].Enabled = true
			end
		elseif clone.Head["4"].Enabled == true then
			if game.Players.LocalPlayer.Character:FindFirstChild("muffin") then
				ProximityPromptTriggered = true
				clone.Head["TY"].Enabled = true
				clone.Head.ProximityPrompt.Enabled = false
			else
				print("You don't have the correct tool!")
				ProximityPromptTriggered = false
				clone.Head["4"].Enabled = false
				clone.Head["NO"].Enabled = true
				wait(2)
				clone.Head["NO"].Enabled = false
				clone.Head["4"].Enabled = true
			end
		end
	end)
	while not ProximityPromptTriggered do
		wait()
	end

	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false
	end
	clone.Head["TY"].Enabled = true
	anim:Play()
	clone.Humanoid:MoveTo(workspace.start.Position)
	wait(2)
	anim:Stop()
	if clone.Head["1"].Enabled == true then
		clone.Head["1"].Enabled = false
	elseif clone.Head["2"].Enabled == true then
		clone.Head["2"].Enabled = false
	elseif clone.Head["3"].Enabled == true then
		clone.Head["3"].Enabled = false 
	elseif clone.Head["4"].Enabled == true then
		clone.Head["4"].Enabled = false 
	elseif clone.Head["TY"].Enabled == true then
		clone.Head["TY"].Enabled = false 

		end
	
	clone:Destroy()

	started = false
	
	wait(2)
	
	end
1 Like

That works a lot better, thanks!! I truly appreciate all your help! :sparkling_heart: However, after the 1st clone is destroyed, the countdown just ends instead of respawning/re-cloning the clone.

https://vimeo.com/793591138

try getting rid of the started = false after the clone gets destroyed.
you already set started to false up top

2 Likes

Thank you! It works now :slight_smile:

I appreciate all of your help SO very much!!!

1 Like

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