Same player getting added to the table constantly

local functionsModule = require(script.ModuleScript)

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local gameFolder = ReplicatedStorage.Game
local status = gameFolder.Status

local CONSTANTS = {
	requiredPlayers = 4
}

while wait(1) do
		
	local players = {}

	repeat wait(1)
		
	for _, player in ipairs(Players:GetPlayers()) do
		if player then
			table.insert(players, player)
			print(string.format("Player was added to Queue: %s", player.Name))
	    end
	end

	status.Value = string.format("Waiting for enough players ( %s %s %s %s", #players, "/", CONSTANTS.requiredPlayers, " )")
		
	until #players >= CONSTANTS.requiredPlayers
end

So, for some reason my script adds me again and again in the players table when it is supposed to add me only the first time, it doesn’t seem to work as expected.

Are you trying to use this to make some sort of round system?

You haven’t checked to see if the player is already in a table. Add an if statement:

if not table.find(players,player) then
    --code
end
1 Like

Replace your part of a script with this one:

for _, player in ipairs(Players:GetPlayers()) do
    if player and not table.find(players,player) then
		table.insert(players, player)
		print(string.format("Player was added to Queue: %s", player.Name))
    end
end