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

Getting Player Amount help?

So ive tried :

local v = game.Players.Value 

Didnt work,Can anyone help? I'd just like to get the amount of players present at the current time. If you cant do that,Then inside a team could also help :P

2 comments

0

Please do your research! Looking up 'Players' on the wiki and scrolling through Properties would have given you the answer!

0

I did that,It didnt answer my question when i studied the wiki page and tried it.

2 answers

✓ Accepted answer

Just use this

local NumPlayers = game.Players.NumPlayers
print (NumPlayers)

It's easier then you think :)

1 comment

1

Thanks :P

Of course local v = game.Players.Value wouldn't work. game.Players is not a Value Object, and game doesn't have a Players property. ;-;

One of two good ways (I recommend the second function, as it causes less activity in the game):

while wait(.25) do					-- This will execute the code block every .25 seconds (continuous, spurring some activity in the game)
	local PlayersInGame = game.Players:GetChildren()		-- I believe you can use :GetPlayers() too?
	print(#PlayersInGame)
end

Or

game.Players.ChildAdded(function ()	-- This will execute the function every time a Player is added (won't be as continuous, therefore less activity)
	local PlayersInGame = game.Players:GetChildren()		-- :GetChildren() method will get everything in the Players instance. I believe you can use :GetPlayers() too?
	print(#PlayersInGame)		--Will print the amount of players in the instance
end)

EDIT

Apparently there's a "NumPlayer" property. Heheh. Didn't know that. ('._.)

print(game.Players.NumPlayers)

I'm keeping the other functions up for reference on how to find the number of objects in a given object. You can try and utilize the functions to self-update an object with a string value for display (such as the Text property for TextLabel)

E.g.

game.Players.ChildAdded(function ()
  local NumberOfPlayers = game.Players.NumPlayers
  script.Parent.TextLabel.Text = NumberOfPlayers .. " are in this game."
end)

3 comments

0

wrong. game does have a players property, you can see it under Workspace

0

Technically, it's a child of 'game.'

0

Er, I mean instance.

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