I was wondering if there was a way to detect/fire a script every time a new player joins your game. Essentially, I am hoping for a live counter that will detect new users and it doesn't matter what server you are in. Please let me know.
There is a function built into players called PlayerAdded
, what it does is exactly what it says it does, it senses for new players joining. The way you could use this for a player count is put a value in ServerStorage and put the code below in a script inside ServerScriptService:
game.Players.PlayerAdded:Connect(function() -- Firing every time a player joins game.ServerStorage.ValueName.Value = game.ServerStorage.ValueName.Value + 1 --This math makes the value one more than what it was before. end)
The only problem with just having that is that if a player leaves and joins again, it will keep going up but never down. Thats why there is a function called PlayerRemoving
which senses the exact opposite of PlayerAdded
. The way you could get a value to go down with this is by putting these lines into the same script we already made (after the "end)").
game.Players.PlayerRemoving:Connect(function() -- Firing every time a player leaves game.ServerStorage.ValueName.Value = game.ServerStorage.ValueName.Value - 1 --This math makes the value one more than what it was before. end)
Hope I helped.
-Cmgtotalyawesome