Table.insert not working as intended?

I’m not that familiar with tables but for some strange reason, this just doesn’t work

game.Players.PlayerAdded:Connect(function(plr)
	local function counterround(plrtable)
		plrtable = {}
		table.insert(plrtable, plr.Name)
		for i,v in pairs(plrtable) do
			if i < 2 then
				warn("Not enough players")
			else
				print("More than one player!")
			end
		end
	end
 
	counterround()
end)

Basically, when there’s two players in a server, it still states that there is not enough players however because there should be two items in the table, it should state there is more than one player.
I’ve tried adding delays but nothing has worked

I don’t think you need to use a for loop to check if there are more than 2 instances in the table, you can just use #plrtable, which returns a number how many thing are there


if #plrtable < 2 then
	warn("Not enough players")
else
	print("More than one player!")
end
1 Like

the problem is: the player added because anytime a player joins another function and for i loop is being created

1 Like

to solve it do this!

local function counterround(plrtable)
	plrtable = {}
	table.insert(plrtable, plr.Name)
	for i,v in pairs(plrtable) do
		if #plrtable < 2 then
			warn("Not enough players")
		else
			print("More than one player!")
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	counterround()
end)
2 Likes

as coolyoshi said, you don’t have to make a for loop to basically get the amount of players in a server, and we also need a PlayerRemoving event in case someone leaves the game:

local function onPlayerChanged(plr)
	local i = #game.Players:GetChildren() -- Returns the amount of children game.Players has (amount of players)
	if i < 2 then
		warn("Not enough players")
	else
		print("More than one player!")
	end
end

game.Players.PlayerAdded:Connect(onPlayerChanged) -- Runs the function when a player joins the game
game.Players.PlayerRemoving:Connect(onPlayerChanged) -- Runs the function when a player leaves the game (In case there are 2 players and someone leaves)
2 Likes

oh you are right i forgot the player removed is really special, if you implement a timer before the game starts chek if the player number is more than 1 then start the timer and when the timer goes to 0 check again how many players there are

1 Like

OH i noticed this:

if i < 2 then
	warn("Not enough players")
else
	print("More than one player!")
end

this says more than one player when there are 3 players so do even this!

if i < 1 then
	warn("Not enough players")
else
	print("More than one player!")
end
2 Likes

I’m not sure what you’re saying but the topic creator (the guy who made this topic) wanted to check if there wasn’t only one player in a server, because i < 1 checks if there’s one player or more but if you use i < 2 it checks if there’s 2 players or more

1 Like

in reallity i <= 1 checks if is equal to 1 or more than 1 but i < 1 checks if is more than 1

try it yourself if you don’t belive me :wink:

1 Like

With i < 1:

With i <= 1:

In fact, it’s the inverse :slightly_smiling_face:

1 Like

ok then why to me is different?

Did you try using the same script I used? This probably will change something

no i didn’t because creating a test server to me needs 20 minuites because this computer was meant to use for school

1 <= 1 checks if the number is smaller or equal to the other number. 1 < 1 checks if the number is only smaller than the second number

1 Like

try doing a 1 player test then

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