How do I improve this

function RoundSystem.CheckPlayers()
	local allowedToPlay

	for playerIndex, player in pairs(ALLPlayers) do
		if player:GetAttribute("InMenu") == true then continue end
		if player:FindFirstChild("AFK") and player.AFK.Value == true then continue end

		if playerIndex >= PlayersRequired then
			allowedToPlay = true
		else
			allowedToPlay = false
		end
	end

	return allowedToPlay
end

This is my code for if there is enough players to play. But sometimes if one player still is in the menu it will start. I wan’t it so if there is 2 players for example. and one presses play in the menu. And the other player is still in the menu, the game shouldn’t start. But sometimes it still does even if only one player presses play. I’m guessing it’s something with the playerindex but idk. (Hopefully this makes sense)

By the way “ALLPLAYERS” is a table that inserts a player that joins and removes them if they leave

1 Like

Why have an ALLPlayers table? Why not just use Players:GetPlayers()

Anyways it’s because it doesn’t really count the amount of players with those conditions passed. Let’s say the first two players (1, 2) are off in menu, they would be skipped, that’s fine. The third player has an index of 3 (notice how it’s not one even though it’s the first player that passes the checks), which is more than the required players amount.

This should be the fixed code:

function RoundSystem.CheckPlayers()
	local playersAmount = 0

	for _, player in Players:GetPlayers() do
		if player:GetAttribute("InMenu") then continue end
		if player:FindFirstChild("AFK") and player.AFK.Value then continue end
		playersAmount += 1
	end

	return playersAmount >= PlayersRequired
end
1 Like

thank you so much man, I couldn’t find out why but now I understand. Ty again

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