How to return value of i so the whole system runs in a loop

local textLabel = script.Parent
local lobbyRemaining = 10
local timeRemaining = 10
local InGame = game.Workspace:WaitForChild("InGame")
local InLobby = game.Workspace:WaitForChild("InLobby")
local timeLeft = 10
local lobbyLeft = 10

if InLobby.Value == true then
	for i = 10, 0, -1 do
		wait(1)
		textLabel.Text = "Intermission: "..i
		if i == 0 then
			InLobby.Value = false
			InGame.Value = true
		end
		return i	
	end
end

if InGame.Value == true then
	for i = 10, 0, -1 do
		wait(1)
		textLabel.Text = "Time remaining: "..i
		if i == 0 then
			InGame.Value = false
			InLobby.Value = true
		end
	end
end

Looking to make a small minigame system as a project to learn how to code but I am stuck on this one thing if anyone has any advice how I could get the value of ā€œiā€ back to 10 would be much appreciated.

You probably do not want to set i to 10. It would be better to fire events for this sort of thing, but I suppose for now wrapping it in a infinite loop will do

local InGame = game.Workspace:WaitForChild("InGame")
local InLobby = game.Workspace:WaitForChild("InLobby")
local textLabel = script.Parent

while true do
	if InLobby.Value == true then
		for i = 10, 0, -1 do
			task.wait(1)
			textLabel.Text = "Intermission: "..i
			--return i -- do not return outside of functions!	
		end

		-- set these values after the for loop, do not need to check i == 0
		InLobby.Value = false
		InGame.Value = true
	elseif InGame.Value == true then
		for i = 10, 0, -1 do
			task.wait(1)
			textLabel.Text = "Time remaining: "..i
		end

		InGame.Value = false
		InLobby.Value = true
	else
		error("Not in lobby or game!") -- errors help debugging
	end
end
1 Like

Thank you very much will look into firing events for this