Attempt to index nil with 'Character'

Hello! im making a boxing ring system, and I cant detect when the players health reaches 0 so I can determine who won. Can someone please help? It returns with this error.

Thank you.

while task.wait() do
				if game.Players:FindFirstChild(Player1.Value).Character.Humanoid.Health <= 0 then
					print('player 1 won')
					win(Player2.Value)
				elseif game.Players:FindFirstChild(Player2.Value).Character.Humanoid.Health <= 0 then
					print('player 2 won')
					win(Player1.Value)
				end
			end

This error is caused because game.Players:FindFirstChild(Player1.Value) is returning nil (or the other findfirstchild is returning nil). I recommend ditching the value objects and the findfirstchild entirely, and instead setting the player objects as variables. Also, use connections instead of loops.
Here’s some example code

local player1 = -- player object here, NOT a player name
local player2 = -- player object

local p1c,p2c
p1c = player1.Character.Humanoid.Died:Connect(function()
  p1c:Disconnect()
  p2c:Disconnect()
  
  win(player1)
end)

p2c = player2.Character.Humanoid.Died:Connect(function()
  p1c:Disconnect()
  p2c:Disconnect()
  
  win(player2)
end)
1 Like

Like this?

	local player1 = game.Players:FindFirstChild(Player1.Value)
			local player2 = game.Players:FindFirstChild(Player2.Value)
			
			local p1c
			local p2c
			p1c = player1.Character.Humanoid.Died:Connect(function()
				p1c:Disconnect()
				p2c:Disconnect()

				win(player1)
			end)
			
			p2c = player2.Character.Humanoid.Died:Connect(function()
				p1c:Disconnect()
				p2c:Disconnect()

				win(player2)
				end)

That actually will not work because the root of your problem is the fact that FindFirstChild is returning nil. If you are this insistent on using values, make sure that the value’s value is set to the player’s name.