Why does the if statement work every OTHER round?

Im making an if statement on a round that if there is only 1 player it re starts the intermission(since 2 players are needed to play). I’ve noticed that it works the first time but not the second, then it works the 3rd time, but not the 4th(so on) so i came to a conclusion that the if statement works every other time, and i cant figure out why. All help appreciated

There were also no errors

(NOTE: This is a Server Script and this is not the full script):

local function RoundCountdown()
	local timeleft


	for i = 12, 0, -1 do
		InfoLabel.Text = "Intermission.."..i	
		task.wait(1)
	end

	for i,v in pairs(Players:GetChildren()) do
		table.insert(PlayersInRound, v)
		v.Character:MoveTo(workspace["The Park"].MapSpawn.Position)
		IsInMatch:FireClient(v, true)
	end

	for i = 3, 0, -1 do
		InfoLabel.Text = "Round Starting in "..i.."."
		task.wait(1)
	end

end

local function StartRound()
	matchoccuring = true
	local timeleft
	
	print(#PlayersInRound)
	if #PlayersInRound == 1 then
		InfoLabel.Text = PlayersInRound[1].Name.." won the game!"
		task.wait(2.5)

		for i,v in pairs(PlayersInRound) do
			v.Character:MoveTo(LobbySpawn.Position)
			IsInMatch:FireClient(v, false)
		end
		
		table.clear(PlayersInRound)
		
		RoundCountdown()
	end

Don’t see where PlayersInRound is getting set … blind guess

local timeleft
PlayersInRound = #PlayersInRound

Make sure to reset all your variables, which you did with the table, but also matchoccuring to false (if that’s what it originally was). I’m kinda early, so try out other ppl’s suggestions, but if all else fails you could probably try a separate script which disables and re-enables the main script.

Hi, I would suggest that you rewrite your code from scratch, since I see a few flaws with it.
One of the noteworthy is, that the 12 seconds in Intermission will run, even though there in the meantime is less players available than the amount needed. I’ve written the barebone mechanics of a game loop. I’ve not even made a intermission

local Players = game.Players

local MinimumPlayersToStartGame  = 2
local MinimumPlayersToEndGame = 1
local RoundTime = 120

local function CAP(Status) -- Currently Available Players
	local CAPs = 0
	if Status == "Update" then
		for _, Player in pairs(Players:GetPlayers())
			if not Player:GetAttribute("CurrentlyPlaying") then continue end
			CAPs += 1
		end
	end
	return CAPs
end
local function CPP(Status) -- Currently Playing Players
	local CPPs = 0
	if Status == "Add" then
		for _, Player in pairs(Players:GetPlayers())
			-- You set their CurrentlyAvailable attribute to true, when they're fully ready to play
			-- This can be done when done loading data, or when not in spectating mode. Whatever you prefer.
			if not Player:GetAttribute("CurrentlyAvailable") then continue end
			Player:SetAttribute("CurrentlyPlaying", true)
			CPPs += 1
		end
	elseif Status == "Update" then
		for _, Player in pairs(Players:GetPlayers())
			if not Player:GetAttribute("CurrentlyPlaying") then continue end
			CPPs += 1
		end
	elseif Status == "Remove" then
		for _, Player in pairs(Players:GetPlayers())
			if not Player:GetAttribute("CurrentlyPlaying") then continue end
			Player:SetAttribute("CurrentlyPlaying",false)
		end
	end
	return CPPs
end

while true do
	-- Wait for players
	local CurrentlyAvailable
	repeat
		CurrentlyAvailable = CAP("Update")
		task.wait(1)
	until CurrentlyAvailable >= MinimumPlayersToStartGame
	-- Now we have enough players
	-- Put them in CurrentlyPlaying
	CPP("Add")
	-- Teleport them or whatever you wish to. Also make sure that their "CurrentlyPlaying" attribute
	-- Is set to false whenever they die, you can do that in a PlayerAdded event, we do not need to
	-- check it within this while loop.
	-- Default data
	local Time = RoundTime
	local PlayerWon
	local CurrentPlayers = CPP("Update")
	-- Just a random way of winning
	WinPartOrSomething.Touched:Connect(function(Hit)
		-- Do your own checks to check if it's a player or not, since this is just a example
		if Player then
			PlayerWon = Player
		end
	end
	-- Game loop!
	repeat
		-- Do whatever round specific stuff you want here
		CurrentPlayers = CPP("Update")
		Time -= 1
		task.wait(1)
	until Time == 0 or CurrentPlayers <= MinimumPlayersToEndGame or PlayerWon
	-- After round, if we have a winner, do something.
	if PlayerWon then
		-- Give coins, or display message
		print(PlayerWon.Name.." won the game!"
	else
		print("No-one won the game!"
	end
	-- Reset players here
	CPP("Remove")
end

Thankyou for the responses but i already got a solution, i just moved the if statement to the other function that was executed before the one the if statement was initially in; so it’s working now

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