I have a script that changes a value inside of replicated storage when a player joins or leaves. Here is my script:
wait(1) local Players = game:GetService("Players") --gets players local value = script.Parent.PlayersOnline local PlayerCount = #Players:GetPlayers() --gets the amount of players Players.PlayerAdded:Connect(function() --if a player gets added, PlayerCount = PlayerCount + 1 --increase the playercount by 1 value.Value = PlayerCount end) Players.PlayerRemoving:Connect(function() --if a player gets removed, PlayerCount = PlayerCount - 1 --decrease the playercount by 1 value.Value = PlayerCount end)
When someone joins, the value doesn't change for some reason. Any suggestions?
If you're testing in Play Solo mode in Roblox Studio, sometimes the player loads too fast and the event isn't fired. To fix it, either create a proper local server or add a function to the event and loop through the players to not miss any of them. Example:
function onPlayerAdded(player) --Codes here end for _, player in pairs(game.Players:GetPlayers()) do --looping through the players list onPlayerAdded(player) end game.Players.PlayerAdded:Connect(onPlayerAdded) -- attaching the function to the event
For the PlayerRemoving event, you can also do the same.