Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Issue counting players?

I test the game with a friend, and the Server console keeps printing Not Enough Players, confirming something's not right. I have tried using methods like GetChildren from Players, and for i,v etc.


local plrs = game.Players.NumPlayers
	
while true do
	wait(6)
	if plrs == 2 or plrs >= 2 then
		print("Enough Players. Begininning Minigame process.")
		local RandomChoice = script.Random:GetChildren()
		local RoundChoseWay = RandomChoice[math.random(#RandomChoice)]
		GetRoundChoser(RoundChoseWay)
	else
		print("Not Enough Players")
	end
end



function GetRoundChoser(RoundChoseWay)
	if RoundChoseWay == "Auto" then
		local Minigames = script.Parent.Minigames:GetChildren()
		local MinigameChosen = Minigames[math.random(#Minigames)]
		ExecuteRound(MinigameChosen)
		print(MinigameChosen)
	elseif RoundChoseWay == "Vote" then
		local Minigames = script.Parent.Minigames:GetChildren() -- Delete when Vote system is done.
		local MinigameChosen = Minigames[math.random(#Minigames)]
		-- Vote for minigame todo.
		ExecuteRound(MinigameChosen)
		print(MinigameChosen)
	end
	
end



function ExecuteRound(MinigameChosen)
	if MinigameChosen == "BombSurvival" then
		
	end
	
end

1 comment

0

So the issue is getting the number of players?

3 answers

game.Players.NumPlayers is not a thing. To get the number of players, you do this:

NumPlayers = #game.Players:GetPlayers()

game.Players:GetPlayers() returns a table full of all the players, and the # gets the number of items in a table.

Also, some of this code is inefficient, I'll help clean up a bit.

while wait(6) do -- while wait(6) do is the same as while true do wait(6)
	if NumPlayers >= 2 then -- no need for the ==, because >= checks if it is greater than, or equal to.
		-- do stuff
	else print"Not enough players"
	end
end

Okay, according to your comment, I think I found the issue.

NumPlayers is not updating.

game.Players.PlayerAdded:connect(function() --fires when player added, the perfect reason to change NumPlayers!
	NumPlayers = #game.Players:GetPlayers()
end)
game.Players.PlayerRemoving:connect(function() --fires when player leaves, need this one too
	NumPlayers = #game.Players:GetPlayers()
end)

12 comments

0

game.Players.NumPlayers is a property. And if it wasn't it wouldn't return false, because it didn't know how to get the value.

0

Wow, it is a thing... Also, false? I never saw false in my answer, or the question.

0

game.Players.NumPlayers does not return a true or false. it returns the number of players http://wiki.roblox.com/index.php?title=API:Class/Players

0

using local NumPlayers = #game.Players:GetPlayers() while wait(6) do if NumPlayers >= 1 then print("Enough Players") else print("Not Enough players") end end I still get Not enough Players in server log

0

Also I know that but its not what I meant. The script would kinda return false, I meant an error in the log.

0

the problem with the code above is that it does not update players, you need to create a function to return the number of players, and then use it in the while loop each event.

0

just edited my answer...

0

How do I put the two scripts together.... I can't use local as a variable because the player added and removing is seperate from the loop

0

Nevermind, Why does it not need to be a variable..

0

What? Can you explain what your question has mutated into?

0

relatively instead of trying to get the number of players, you can just create a variable to add or delete depending on whether a person has joined or left. and the variable will always be updated depending on it.

0

That would add complexity where it's not needed.

local NumPlayers = #game.Players:GetPlayers()


while wait(6) do
    if NumPlayers >= 1 then
        print("Enough Players")
    else print("Not Enough players")
    end
end

Doesn't work. Question is still Open.

1 comment

0

Just edited my answer, also to say that an answer is horrid, make the title be Issue counting players?[ignore current answer]

players =game.Players.NumPlayers;

print(players);
while( players ==2)
do
	print("there are 2 players");
	wait(1);
	players = game.Players.NumPlayers;
end

here is an example while loop. to maintain a check.

as you seen in the example you need to update variables,thats what I was hinting at.

while true do
	wait(1)
	players = game.Players.NumPlayers
	if(players == 1) then
		break;
	end
	print(players);
end

this does exactly what you need, just replace the print statement and place your code below. I highly recommend to use this as a function.

3 comments

0

While its the same as 2? Then it wouldn't run at the very start, when only one player is in.

0

yep, it was an example

0

Not like a single number will do much. i would probably realise something like that when My console outputted stuff.

This discussion is preserved as it was. New answers, comments, and votes are closed.