How to subtract a specific amount of a Value if the Player left the Game

Hey Developers,
I was. wondering if it is possible to subtract a specific amount of a Value in Workspace, if a Player left.
Here is the Code I made:

game.Players.PlayerRemoving:Connect(function(player)
	if player.Race.Value == true then
		game.Workspace.RaceHandler.Competitors.Value -= 1
		print("Worked")
	end	
end)

The Race Value gets == true if the Player participated in the Race (works fine). So only if the Player currently participated in the Race and then left the Game the Value goes -1 down.
The reason I am making this is because of a Race System. A value inside a Script inside Workspace always adds +1 if 1 Player is added to the Race. If the competitors died, or just left the Race the Value goes -1. If the Value reached 0 the Race simply ends. Here is the Code I am using for this function:

repeat wait(1)
		script.Time.Value -= 1
		Text2.Value = "Time Left: " ..script.Time.Value
		until script.Time.Value <= 0 or script.Competitors.Value <= 0

The problem is that the Script simply does not work… No “Worked” gets printed in output, even if the Players Race Value was == true.
Any help would be appreciated!

I created a script named RaceHandler and put it in the Workspace with this code and it worked. Are you sure the script is in a service where it can run and the player’s Race value is not being set on the client?

When I tested it, the server printed the message before the game shut down.

In order to test this won’t you need to leave the game closing all code?

Try printing the Player.Race.Value when a player leaves the game, above the if statement. If it is really true then you should get true printed in the output.

1 Like

Thank you for the fast Reply!
Both lines are are in normal Scripts, the player removing line in 1 Script in ServerScriptService and the second line is in a whole system in the script in workspace. Also I am testing it in Studio…

I was originally thinking that it would not be necessary to check if there were not any players left in the game because by the time that there is none, the code would stop and the game would close, but if there are still players in the game and not in the race then it will still be useful.

Code will still run after a game closes, I think that there is like 10 or 60 seconds after all players leave that game code can run to do things like save data.

so i tested the same function, but without the if statement and then it worked. But with the check function it doesnt print anything. i also created an else function and it printed the else function. I checked if my Race Value was set to true
EDIT: i noticed that I am mostly changing the Race Value client sided… i will fix this real quick and tell if it worked!

yep that was the error… sorry for wasting your time :confused: