So basically I am trying to get the number of players with a value in their backpacks called Racing set to true. I know its easy, but the problem is, I'm making a racing game in which the race is supposed to end when everyone finishes, and I do that using a script that sets a value in workspace called PlayersRacing to the amount of racing players that are in the game. The problem is, I can't change the value when someone leaves during the race. (I tried PlayerRemoving, but that doesn't work) So I thought I could make a loop script that makes the PlayersRacing value the current number of players racing that are in the game, but I don't know how.
In first place, setting a value within their backpacks isn't a good idea. If it's a simple game, you can simply put the value in the player. Insert the BoolValue in StarterPlayer and it will be copied to every player instance when it is created.
You can make a while loop to check something like
function CheckRacers() local PlayersRacing = workspace.PlayersRacing PlayersRacing.Value = 0 for i,v in pairs (game.Players:GetPlayers()) do if v.Racing.Value then PlayersRacing.Value = PlayersRacing.Value + 1 end if PlayersRacing.Value == 0 then RaceFinished() end --Basically, if nobody had Racing value true, that means that the function should trigger the race end. end end
EDIT:
function RaceFinished() --Do whatever has to be done once the race finishes. end function CheckRacers() local PlayersRacing = workspace.PlayersRacing PlayersRacing.Value = 0 for i,v in pairs (game.Players:GetPlayers()) do if v.Racing.Value then PlayersRacing.Value = PlayersRacing.Value + 1 end end if PlayersRacing.Value == 0 then RaceFinished() end --Basically, if nobody had Racing value true, that means that the function should trigger the race end. end