How Could I Find How Many Players Are In The Server?

I know there are a lot of topics on here about finding the amount of players in a server, but what I want is to use the amount of players and change the amount of cash being distributed among the players depending on how many players are in the server. Right now, for my game, I have a round system, and at the end of the round players get Gold. I decided that I wanted the gold to be divided by the amount of players in the server to make the game more fair, and I haven’t done any scripting with this specific stuff before, so I’m unsure how to do that. I do have some script and it’s returning no errors. I will attach that below.

local numberOfPlayers = #Players:GetPlayers()

This is where I got the number of players (I’m guessing this is where the issue is)

			local reward = 50 * wave
			for i, player in ipairs(Players:GetPlayers()) do
				player.Gold.Value += reward / numberOfPlayers
				print("There are " .. numberOfPlayers .. " players in the server")

This is where I actually used the number of players and divided the reward among the players. Also, it’s printing out that I have 0 players in the server, even though I do have players in the server, which is why I don’t think this part of script is where the issue is coming from.

When you do #Players:GetPlayers() you call it before anyone is inside the server you should instead make a function that you can call that gets the number of players something like this.

local getNumberOfPlayers = function()
	return #Players:GetPlayers()

local numberOfPlayers = getNumberOfPlayers()

Make sure you call it when you need to use it.

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