How do I figure out the amount of players in the game?

Im trying to make a game cald bomb tag, and I need to find the number of players in the game to determine if the match is going to start or not.

so how do I figure out the amount of players in the game?

3 Likes

You could get all the players in the game by doing game.Players:GetChildren() and using the len operator for tables (which is a #) as such: local playerCount = #game.Players:GetChildren(). From there you can put it in an if statement to see if there’s enough players.

7 Likes

GetPlayers is actually a better method.

Try this:

local playerCount = #game.Players:GetPlayers()
4 Likes

What’s a Len operator and what does it do?

And also, for the if statement, will I put something like

if playerCount > 1 then
 -- start game
end

and also, what do I do if a player leaves the match and their is 1 more person just their? won’t the game have to be waited out?

The reason why is because if there is a another instance in game.Players, the :GetChildren() method will detect that, when the instance isn’t a player. The : GetPlayers() method is way safer, because it only counts players. Use :GetPlayers() whenever you’re dealing with this.

3 Likes

Does your game have a timer? If so, you can have a loop running, that while the game is started, whenever a player leaves/joins using .PlayerRemoved check the amount of players in game.

The problem you’re having right now is a problem every scripter has to go through. Only real scripters can get through problems and solve them. You have to learn how to consider every option in the game.

The length operator (’#’), is an operator that returns the amount of “things” in a table. say you had a table:

local players = {"Bill", "John", "Maria"}

-- if you say:

print(#players)

This will print 3, because there are three “things” in the players table.

2 Likes

Why would you opt to use :GetChildren() instead of :GetPlayers()? The latter omits non-player objects.

4 Likes

Im trying to get all players and take them back to spawn but, its only working for the NPC’s and not for actual players in the game.

Theirs no error’s eathier.

local function EndRound()
	local Map = game.Workspace:WaitForChild("Map")
	
	for i, v in pairs(Map:GetChildren()) do
		v:Destroy()
	end
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		for index, value in pairs(v:GetChildren()) do
			if value.Name == "Humanoid" then
				local HRP = value.Parent:WaitForChild("HumanoidRootPart")
				
				HRP.Position = Vector3.new(32.287, 278.1, -257.829)
				
				for bruh, bruh2 in pairs(game.Players:GetChildren()) do
					if bruh2.Name == HRP.Parent.Name then
						local DeathBool = bruh2:WaitForChild("Death")
						
						DeathBool = true
					end
				end
			end
		end
	end
end

Can you explain what you’re trying to fully accomplish here?

len operator will give you the length(number) of how many items are in a table
like
if you had a table full of 5 strings and used

local tblLen = #stringTbl

it will give you the number 5

If you want to put the players back to spawn maybe do:

for i,v in ipairs(game.Players:GetPlayers()) do
  if v.Character then
    v.Character:MoveTo(Vector3.new(32.287, 278.1, -257.829))
  end
end
1 Like

Im making a bomb tag game, and theirs a game loop that makes it so that the game will go for 3 minutes and then, it’s supposed to end after those minutes and then restart.

The endgame () function is supposed to end the game, the issue is, it won’t teleport all of the players to the lobby, which is the position/vector3 thing I set up.

When the round ends, the players are supposed to be dead, so they won’t be considered still in the match. And I find the player’s character to get their position, and also to make the player being dead true.

Maybe do this except check if they’re still alive?

  if v.Character and v.PlayerAlive.Value==true then
    v.Character:MoveTo(Vector3.new(32.287, 278.1, -257.829))
  end

I thought the move to function was for the pathfinding option and also, all players have to be considered dead at the end of the game to make sure their not considered still in the game after the game ends.

MoveTo is a pathfinder when you move a humanoid. When you MoveTo the whole character model it is a teleport

1 Like

But why can’t I just change the humanoid Root Part’s position?

You can, and i don’t think it will bring any kind of issues. However it is better to teleport the player as it was intended too.

Can you specify this reason in particular?

Whenever a player joins, create a “PlayerAlive” BoolValue object inside the Player. And then just change it to true when the game starts and false when they die.
v.Character in this case is a model. There are a ton of ways to move a model:


From these two functions, you can do the following:

local teleTo = Vector3.new(32.287, 278.1, -257.829)
local ccf = v.Character:GetPivot()
v.Character:PivotTo(ccf - ccf.Position + teleTo)
-- or you can do
v.Character:PivotTo(CFrame.new(teleTo)) -- this doesn't use :GetPivot()


With this you can do:

local teleTo = Vector3.new(32.287, 278.1, -257.829)
local cp = v.Character.HumanoidRootPart.Position
v.Character:TranslateBy(teleTo - cp)


With this you can do:

local teleTo = Vector3.new(32.287, 278.1, -257.829)
local ccf = v.Character.PrimaryPart.CFrame
v.Character:SetPrimaryPartCFrame(ccf - ccf.Position + teleTo)
-- or you can do
v.Character:SetPrimaryPartCFrame(CFrame.new(teleTo))


With this function, which I used in the example you can do:

local teleTo = Vector3.new(32.287, 278.1, -257.829)
v.Character:MoveTo(teleTo) -- the orientation should be unchanged

I don’t know why Roblox has this many functions to just move a model.

Now since it’s a rig, you can set the Position of the root part of the rig and it will move the whole rig. However I believe if you set the CFrame it will not move the whole rig.