How can I change the players team depending on their leaderstats

I want to make it so that when a player reaches lets say, 5 wipeouts, their team is changed to spectators

I am unsure of how I can do this in a serverscript and what I have tried has not worked and has not errored.

I have tried using this small script though,

game.Players.PlayerAdded:Connect(function(player)
	if game.Players.player.leaderstats.Wipeouts.Value == 2 then
		game.Players.player.Team = game.Teams.Spectators
	end
end)

Any idea on what I can do?

So to do this, you’d have to detect each time the Wipeouts of the player changes, instead of checking immediately when the player joins the game.

With this, I’m assuming you’re adding the leaderstats and the Wipeouts in another script.

local spectatorWipeout = 2
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local wipeouts = leaderstats:WaitForChild("Wipeouts")
	wipeouts:GetPropertyChangedSignal("Value"):connect(function(newValue)
		if newValue == spectatorWipeout then
			player.Team = game.Teams.Spectators
		end
	end)
end)