Table index count through remote event

I am learning how to make a player lobby system through UI, however I am stuck on this problem that I can’t properly update player count on each existing lobby for all clients. Whenever a player joins a lobby, a remote event is fired and the player gets added to a player count table in the serverscript where of course the lobby creator already is, I connect back in the localscript and with the new table values so I can update the player count for the lobby but it seems that I can’t think of a way to do it correctly. I would greatly appreciate the help and if someone could correct me.

LocalScript

game.ReplicatedStorage.JoinLobbyRemoteEvent:FireServer(player, localplayerusername, username)
print(localplayerusername.." has joined "..username.." lobby")	


game.ReplicatedStorage.JoinLobbyRemoteEvent.OnClientEvent:Connect(function(localplayerusername, Playerlist)

	local PlayerCount = #Playerlist
	CreatedLobbyPlayerCountLabel.Text = PlayerCount.."/4" 

end)

Serverscript

game.ReplicatedStorage.JoinLobbyRemoteEvent.OnServerEvent:Connect(function(players, localplayerusername, username)
	
	
	local Playerlist = {username} 
	print("event received from the client joining lobby") 
	table.insert(Playerlist, 2, localplayerusername)
	
	game.ReplicatedStorage.JoinLobbyRemoteEvent:FireAllClients(players, localplayerusername, Playerlist)
	
	
end)

Of course this is all part of a much bigger code and if there is some confusion about some of the variables then I will explain them.

Error message : Attempt to get length of a number value.

Try store the playerlist in a variable instead of passing it every script.

Also, not to be that guy, but you need to put end) on one of your connections.

1 Like

When calling FireServer, the local player is automatically passed as the first parameter.
As you are providing the player manually as well, it means the values in the received parameters are not what you expected (players and localplayerusername become the player, username becomes the localplayerusername, and localplayerusername gets vaporised.

To fix it, just replace your FireServer call with

:FireServer(localplayerusername, username)

Likewise, when doing FireAllClients, you don’t need to provide the player arguments.
(If you want it to go to a specific player, then use FireClient instead.)

1 Like