How do i loop thru a intermission and game in progress issue

So i have a game that has a timer and it ends once it hits 0:00. I want it to intermission for 15secs and teleport to the lobby, sometimes it bugs out at intermission: 0 or the timer is played in the lobby. BTW: THE TIMER GOING FAST IS ON PURPOSE, IT IS FOR THE VIDEO AND TESTING PURPOSES
Vid: https://youtu.be/TviFoH4R31k

Please help, I have been trying to fix this for a few days! :smiley:

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Lobbyspawn = game.Workspace.Lobby:WaitForChild("LobbySpawn")
local RoundSpawn1 = game.Workspace.SpawnLocations:WaitForChild("LobbyTP")
local ts = game:GetService("Teams")
local Loadedevent = ReplicatedStorage:WaitForChild("LoadedEvent")
local TimerEvent = ReplicatedStorage:WaitForChild("TimerEvent")
local Players = game:GetService("Players")
local checkpointsFolder = workspace:WaitForChild("Checkpoints")
local checkpoints = checkpointsFolder:WaitForChild("0")

local timer = script.Parent
local minutes = 1
local seconds = 0

local intermissionlength = 15
local intermissionEnded = false

local Roundended = false



Loadedevent.OnServerEvent:Connect(function(plr)
	local function intermission()
		for _, player in pairs (game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = Lobbyspawn.CFrame
			repeat
				for i = intermissionlength, 0, -1 do
					wait(1)
					print(i)
					timer.Text = "Intermission: "..i
					player.Team = game.Teams.Level0
						if i == 0 then
							Roundended = false
							intermissionlength = 0
							
						end
				end
			until intermissionlength == 0
		end
	end
	
	local function Timer()
		task.wait()
		repeat
			if seconds <= 0 then
				minutes = minutes - 1
				seconds = 59
			else
				seconds = seconds - 1
			end
			task.wait()
			if seconds <= 9  then
				timer.Text = tostring(minutes)..":0"..tostring(seconds)
			else
				timer.Text = tostring(minutes)..":"..tostring(seconds)
			end
		until minutes <= 0 and seconds <= 0
		if minutes <= 0 and seconds <= 0 then
			minutes = 1
			seconds = 0
			Roundended = true
			if Roundended == true then	
				intermissionlength = 15
			end
			intermission()
		end
	end
	Timer()
	if intermissionlength == 0 then
		for _, player in pairs (game.Players:GetChildren()) do
			local char = player.Character
			char.HumanoidRootPart.CFrame = RoundSpawn1.CFrame
		end
		Timer()
	end
	TimerEvent:FireClient(plr)
end)

I’m not sure why you are doing a for loop in a repeat-until inside another for loop. Anyways, you are calling Timer() function just two times. One time when you call Timer() before the if statement and the second time when you call it again under if intermissionlength == 0 then the loop stops. You can keep calling it by:

while true do
	Timer()
end

This should keep the timer loop on.

You probably don’t need the repeat-until before the for i = intermissionlength, 0, -1 do

Thanks for responding. Why dont i need the repeat until? im trying to learn more about scripting

Both of them have differences but work the same nonetheless.

A while loop will continue to run until the condition given is false.

local i = 1
while i < 5 do
	-- Do something
	i = i + 1
end

Hence, having a while true do loop will make it continue forever.

while true do -- DANGEROUS
	-- Do something
end

Repeat until loops will continue to run until condition given is true.

local i = 1
repeat
	-- Do something
	i = i + 1
until i >= 5

Hence, having a repeat until false loop will make it continue forever.

repeat
	-- Do something
until false -- DANGEROUS
1 Like

Why did u put dangerous? will it harm my script…

Unless the loop contains any yielding commands (e.g. task.wait()), it’s going to crash your device if it is executed on the Client, and same goes for the server.

1 Like

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