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

player counter does not return the correct amount of player in game?

I've encounter a problem where this simple player counter prints 0 instead of 1 when there is one person that is currently in the game.

local players = game.Players
local playercount = #(players:GetPlayers())




print (playercount)

Am I doing something wrong in this script?

Any help is greatly appreciated

2 comments

0

did u test it in Play Solo? maybe the script ran before the player was actually added

0

Use PlayerAdded and a for loop to continuously run through players then “print” or whatever function you want, the value of the return

1 answer

✓ Accepted answer

You should make a function for this so it's much clean if you know what I mean.

function get_players()
	local count = 0

	for _, a in pairs(game:service'Players':players()) do --// gets all the players
		count = count + 1 --// adds 1 to the count if a player was found
	end

	return count --// returns the number of players
end

You can then do

local amount_of_players = get_players() --// now amount_of_players will be the amount of players in your server

print(amount_of_players)

3 comments

0

Accept if this helped, thanks.

0

you forgot parentheses for get_players and i think you forgot the 'Get's before service and players also capitalization. you dont need a for loop #game.Players:GetPlayers() will do in just 1 line. im not trying to pick on you just helping OP correct some mistakes in there

0

:players() works too?

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