How do I do this?

How would i make it to detect if one of the players in the 1v1 dies it makes them both teleport back to spawn?

local playerTable = game:GetService(“Players”):GetPlayers()
local button = workspace.Button.Button
local player1Spawn = workspace.Spawn1
local player2Spawn = workspace.Spawn2
button.MouseClick:Connect(function()
if (#playerTable < 2) then
print(“not enough players”)
else

  print("enough players")
  local randomPlayer1 = playerTable[math.random(1, #playerTable)]
  table.remove(playerTable, table.find(playerTable, randomPlayer1))
  randomPlayer1.Character.HumanoidRootPart.CFrame = player1Spawn.CFrame


  local randomPlayer2 = playerTable[math.random(1, #playerTable)]
  randomPlayer2.Character.HumanoidRootPart.CFrame = player2Spawn.CFrame
  
  
  print(randomPlayer1.Name ..   randomPlayer2.Name)

end
playerTable = game:GetService(“Players”):GetPlayers()
end)

quick script i put together, haven’t tested so I do not know if it works. Remember to change the (your spawn) to be the location of where they need to teleport to

local playerTable = game:GetService(“Players”):GetPlayers()
local button = workspace.Button.Button
local player1Spawn = workspace.Spawn1
local player2Spawn = workspace.Spawn2
button.MouseClick:Connect(function()
if (#playerTable < 2) then
print(“not enough players”)
else

  print("enough players")
  local randomPlayer1 = playerTable[math.random(1, #playerTable)]
  table.remove(playerTable, table.find(playerTable, randomPlayer1))
  randomPlayer1.Character.HumanoidRootPart.CFrame = player1Spawn.CFrame


  local randomPlayer2 = playerTable[math.random(1, #playerTable)]
  randomPlayer2.Character.HumanoidRootPart.CFrame = player2Spawn.CFrame

  
  while true do
  if randomPlayer1.Character.Humanoid.Health == 0 or randomPlayer2.Character.Humanoid.Health == 0 then
   randomPlayer1.Character.HumanoidRootPart.CFrame = (your spawn).CFrame
   randomPlayer2.Character.HumanoidRootPart.CFrame = (your spawn).CFrame
   break
  end
  end  

  print(randomPlayer1.Name ..   randomPlayer2.Name)
end
playerTable = game:GetService(“Players”):GetPlayers()
end)

This causes unnecessary throttling and lag, check out Humanoid.Died.

local spawn1 = workspace.spawn1
local spawn2 = workspace.spawn2

local function start(players)
	local p1 = players[math.random(1, #players)]
	local p2 = players[1]
	if not p1 or not p2 then return end

	local c1 = p1.Character
	local c2 = p2.Character
	if not c1 or not c2 then return end

	local h1 = c1:WaitForChild("Humanoid")
	local h2 = c2:WaitForChild("Humanoid")
	if not h1 or not h2 then return end

	local function ended()
		c1.HumanoidRootPart.CFrame = --[[original spawn]]
		c2.HumanoidRootPart.CFrame = --[[original spawn]]

		-- you can add extra code here as well
	end

	h1.Died:Connect(ended)
	h2.Died:Connect(ended)

	c1.HumanoidRootPart.CFrame = spawn1.CFrame
	c2.HumanoidRootPart.CFrame = spawn2.CFrame
end

What would i do to detect if one of the players in the 1v1 dies it makes them both teleport back to spawn?

What would i do to detect if one of the players in the 1v1 dies it makes them both teleport back to spawn?

What would i do to detect if one of the players in the 1v1 dies it makes them both teleport back to spawn?

What would i do to detect if one of the players in the 1v1 dies it makes them both teleport back to spawn?