Script isn't working?

I have a serverscript inside of server script service and I want it to detect when a player changes to the prisoner team and if so, to change their leaderstats “Prison Time” to 90 and go down 1 ever second until it reaches 0. When it reaches 0, I want the script to change their team back to civilian.

The issue is that the script isn’t changing their value in leaderstats at all. It detects that they changed teams but it doesn’t recognize that they are on the prisoner team.

My code

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetPropertyChangedSignal("Team") then
		print("Team change detected from "..Player.DisplayName)
		if Player.TeamColor == BrickColor.new("Really black") then
			print(Player.DisplayName.." has been teamed into the prisoner team")
			Player.leaderstats["Prison Time"].Value = 90
			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
		end
	end
end)

Is there an error in the script or something I forgot?

1 Like

Anybody know what I did in the script or…?

The Player:GetPropertyChangedSignal(“Team”) is your problem right here. It is an event and not a condition, so you need to rewrite it like this:

game.Players.PlayerAdded:Connect(function(Player)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		print("Team change detected from "..Player.DisplayName)
		if Player.TeamColor == BrickColor.new("Really black") then
			print(Player.DisplayName.." has been teamed into the prisoner team")
			Player.leaderstats["Prison Time"].Value = 90
			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
		end
	end)
end)

I want it to end at 0 but it continues and when i added “repeat until” it crashed saying “script timeout”

game.Players.PlayerAdded:Connect(function(Player)
	Player:GetPropertyChangedSignal("Team"):Connect(function()
		print("Team change detected from "..Player.DisplayName)
		if Player.TeamColor == BrickColor.new("Really black") then
			Player.PlayerGui.MainMenu.Frame.Teams.Visible = false
			print(Player.DisplayName.." has been teamed into the prisoner team")
			Player.leaderstats["Prison Time"].Value = 90
			while wait(1) do
				Player.leaderstats["Prison Time"].Value -= 1
				repeat until Player.leaderstats["Prison Time"].Value == 0
			end
			if Player.leaderstats["Prison Time"].Value == 0 then
				Player.Team = game.Teams.Civilian
				Player:LoadCharacter()
			end
		end
	end)
end)