How can I check once a player reaches 0 in leaderstats?

So I have a server script inside server script service that checks once a player is teamed into the prisoner team. If so, it will make their “Prison Time” value in leaderstats 90 and go down 1 every second.

How can I check once the value reaches 0?

PS: Can I do this in the loop or outside the loop and what do I do to check?

Code:

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetPropertyChangedSignal("Team") then
		if Player.Team == game.Teams.Criminal then
			Player.leaderstats["Prison Time"].Value = 90
			while true do
				Player.leaderstats["Prison Time"].Value = Player.leaderstats["Prison Time"].Value - 1
			end
			if Player.leaderstats["Prison Time"].Value == 0 then
				Player.Team = game.Teams.Civilian
				Player:LoadCharacter()
			end
		end
	end
end)

Does anybody know? Not sure if this is correct either :man_shrugging:

You should do it in the loop, why? Well, the script won’t go further then that loop-

And don’t forget to put a . infront of [“Prison Time”]

And don’t forget to put a . infront of [“Prison Time”]

Technically, you don’t need a “.” in front of a multiple word property. It works with [“Prison Time”] without the “.”.

they are infront what are you even on about

main issue is you didn’t add a wait(1)
you can do

while wait(1) do
    ...
end

or

while true do
    ...
    wait(1)
end

everything else looks fine

Ah forgot that. How can I find out when the value reaches 0? Because when it reaches 0, I want the player to change teams back to civilian.

it is better to do this

while true do
	Player.leaderstats["Prison Time"].Value -= 1
end
1 Like

do this check inside the loop so it should look like this

while wait(1) do
	Player.leaderstats["Prison Time"].Value -= 1
	if Player.leaderstats["Prison Time"].Value == 0 then
		Player.Team = game.Teams.Civilian
		Player:LoadCharacter()
	end
end

to make the code more clean you could do this

local PrisonTime = Player.leaderstats["Prison Time"]
while wait(1) do
	PrisonTime.Value -= 1
	if PrisonTime.Value == 0 then
		Player.Team = game.Teams.Civilian
		Player:LoadCharacter()
	end
end
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		if Player.Team == game.Teams.Criminal then
			Player.leaderstats["Prison Time"].Value = 90
			while Player and wait(1) do
                if Player.leaderstats["Prison Time"].Value <= 0 then
                    break
                end

				Player.leaderstats["Prison Time"].Value -= 1
			end

			Player.Team = game.Teams.Civilian
			Player:LoadCharacter()
		end
	end)
end)

You could also do

repeat
    Player.leaderstats["Prison Time"].Value -= 1
    wait(1)
until Player.leaderstats["Prison Time"].Value == 0

This will decrease the value by 1 each second until it reaches 0

1 Like