How would I detect when a player has respawned?

I have two players, player1 and player2. I want to make two if statements on whether player1 has respawned or player2 has respawned. But i’m not sure how to do this. Any help would be greatly appreciated!

Here is my script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Team1
local team2 = workspace.Arena1Team2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil
local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")

local function startEvent()

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	local ArenaScore1 = player1.PlayerGui.ArenaScore
	local ArenaScore2 = player2.PlayerGui.ArenaScore
	
	ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
	ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
	
	ArenaScore1.Enabled = true
	ArenaScore2.Enabled = true

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = player1.Character
	local char2 = player2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
	
	player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
	player2.RespawnLocation = game.Workspace.Arena1Player2Spawn
	
	local character1Loaded = false
	local character2Loaded = false


	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills > 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)
				player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
				wait(2)
				char1 = player1.Character
				local humanoid1 = char1.Humanoid
				
				ArenaScore1 = player1.PlayerGui.ArenaScore
				ArenaScore2 = player2.PlayerGui.ArenaScore
				
				ArenaScore1.Enabled = true
				ArenaScore2.Enabled = true

				plr2CurrentKills = plr2CurrentKills + 1
				
				ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
				ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills

				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5

				char2.Humanoid.Health = 100

				Sword1.Parent = player1.Backpack
				humanoid1:EquipTool(Sword1)
				
				char2.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				wait(2)
				char2 = player2.Character
				local humanoid2 = char2.Humanoid
				
				ArenaScore1 = player1.PlayerGui.ArenaScore
				ArenaScore2 = player2.PlayerGui.ArenaScore
				
				ArenaScore1.Enabled = true
				ArenaScore2.Enabled = true
				
				plr1CurrentKills = plr1CurrentKills + 1
				
				ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
				ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills

				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5

				char1.Humanoid.Health = 100

				Sword2.Parent = player2.Backpack
				humanoid2:EquipTool(Sword2)

				char1.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
			end)
		end
	end		
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		part1.Transparency = 1
		part1.CanCollide = false
		part2.Transparency = 1
		part2.CanCollide = false
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

part1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player1 = Player
		end	
	end
end)

part1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

part2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player2 = Player
		end	
	end
end)

part2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end

using the humanoid, for example, when the player’s humanoid has 0 health

1 Like

Try This:

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	print(character.Name .. " has spawned")
end

local function onCharacterRemoving(character)
	print(character.Name .. " is despawning")
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Heres Where I Got It

1 Like

Like this? It’s messing up my script for some reason and i’m not sure what I have done wrong…

local function startEvent()

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	local ArenaScore1 = player1.PlayerGui.ArenaScore
	local ArenaScore2 = player2.PlayerGui.ArenaScore
	
	ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
	ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
	
	ArenaScore1.Enabled = true
	ArenaScore2.Enabled = true

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = player1.Character
	local char2 = player2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn
	
	player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
	player2.RespawnLocation = game.Workspace.Arena1Player2Spawn
	
	local character1Loaded = false
	local character2Loaded = false


	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end
					
					ArenaScore1.Enabled = false
					ArenaScore2.Enabled = false
					
					player1.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					player2.RespawnLocation = game.Workspace.SpawnLocs.SpawnLocation1
					
					char1.Humanoid.Health = 0
					char2.Humanoid.Health = 0
					
					plr1CurrentKills = 0
					plr2CurrentKills = 0
					
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)
				player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
				char1 = player1.Character
				local humanoid1 = char1.Humanoid
				
				ArenaScore1 = player1.PlayerGui.ArenaScore
				ArenaScore2 = player2.PlayerGui.ArenaScore
				
				ArenaScore1.Enabled = true
				ArenaScore2.Enabled = true

				plr2CurrentKills = plr2CurrentKills + 1
				
				ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
				ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills

				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5

				char2.Humanoid.Health = 100
				
				if char1.Humanoid.Health == 100 then
					Sword1.Parent = player1.Backpack
					humanoid1:EquipTool(Sword1)

					char2.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
				end
				wait(1)
			end)
			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				char2 = player2.Character
				local humanoid2 = char2.Humanoid
				
				ArenaScore1.Enabled = true
				ArenaScore2.Enabled = true
				
				plr1CurrentKills = plr1CurrentKills + 1
				
				ArenaScore1.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills
				ArenaScore2.ScoreBoard.Scores.Text = plr1CurrentKills..":"..plr2CurrentKills

				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5

				char1.Humanoid.Health = 100
				
				if char2.Humanoid.Health == 100 then
					Sword2.Parent = player2.Backpack
					humanoid2:EquipTool(Sword2)

					char1.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player1Spawn.Position
					)
				end
				wait(1)
			end)
		end
	end		
end

Simply use the function CharacterAdded, when the players character is added into the workspace, should run.

Something like this:

local Players = game.Players
Players.PlayerAdded:Connect(function(plr) -- our Player
plr.CharacterAdded:Connect(function(char) -- Players Character
print("Hi "..plr.Name)
end)
end)

1 Like

I’m trying to do it with a specific player that is already in the game. Any idea on how I can do this. I’m trying to detect when they RESPAWN rather than join the game but i’m not sure how to do it.

Like everybody said, just use player.CharacterAdded. It fires when their character spawns in.

1 Like

you have to put wait(7) after the humanoid’s health reaches 0 or wait(7.5) if wait (7) doesn’t work (7 seconds for the character to respawn after reset)

1 Like

I tried it and it didn’t work for some reason

Put a Server Script into ServerScriptService and copy the code, may work after that

Edit: It does