My script isn't recognizing the player after they die

Hi, my script works perfectly fine until one of the players (player1 or player2) dies. It doesn’t seem to be storing the player as a variable, because it says it gets nil when it trys to do player2.Character.Humanoid.Health = 100

Any ideas how I could fix this? Thanks!

local function startEvent()

game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

local plr1CurrentKills = 0
local plr2CurrentKills = 0

local SwordGiver = game.ServerStorage.Sword

local player1 = standingOnArena1Player1
local player2 = standingOnArena1Player2

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

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

				if plr1CurrentKills == 5 then
					winner = player1
				end
				if plr2CurrentKills == 5 then
					winner = player2			
				end			

				player1.RespawnLocation = game.Workspace.SpawnLocation
				player2.RespawnLocation = game.Workspace.SpawnLocation

				if winner == player1 then
					Sword1:Destroy()
					player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
				end

				plr1CurrentKills = 0
				plr2CurrentKills = 0

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills


				part1.Transparency = 0
				part1.CanCollide = true
				part2.Transparency = 0
				part2.CanCollide = true
			end
			return true
		end

		connections[1] = player1.CharacterRemoving:Connect(function(hit)
			
			char1 = player1.Character

			local player1 = game.Players:GetPlayerFromCharacter(char1)

			local humanoid1 = player1.Character.Humanoid

			plr2CurrentKills = plr2CurrentKills + 1

			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

			player2.Character.Humanoid.Health = 100

			if disconnectConnections(plr2CurrentKills) then return end

			wait(1)

			player2.Character.HumanoidRootPart.CFrame = CFrame.new(
				Arena1Player2Spawn.Position
			)
			player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
			wait(1)
			local Sword1 = SwordGiver:Clone()
			Sword1.Parent = player1.Backpack

		end)
		connections[2] = player2.CharacterRemoving:Connect(function(hit)
			
			char2 = player2.Character

			local player2 = game.Players:GetPlayerFromCharacter(char2)

			local humanoid2 = player2.Character.Humanoid

			plr1CurrentKills = plr1CurrentKills + 1

			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
			game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

			if disconnectConnections(plr1CurrentKills) then return end

			player1.Character.Humanoid.Health = 100

			player1.Character.HumanoidRootPart.CFrame = CFrame.new(
				Arena1Player1Spawn.Position
			)
			player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
			wait(1)
			local Sword2 = SwordGiver:Clone()
			Sword2.Parent = player2.Backpack		

		end)
	end
end		

end

Same as I stated in your other post about this same script…

My loop only works the first couple times - #2 by Scottifly

You are setting the char1 and char2 variables at the beginning of the script and this only runs once. This sets that variable to the player’s character in the workspace, not in the Players service in the workspace.
When a player dies the character in the workspace is destroyed. A new character is loaded into the workspace from the Player service.
This means your char1 or char2 variable is referencing something that is not in the workspace anymore.

You need to make sure to set char1 and char2 somewhere in your script so that when char1 or char2 dies, the variable gets reset to the new player character in the workspace.

3 Likes

Oh sorry I didn’t really understand what you were saying before. Thank you very much, how would would I reset the variable to the new player character?

I’m not 100% sure.
Check out the way other people do it with fighting scripts (or any kind of death script) in the forums.
Either that or use the create.roblox.com documents to search how Humanoids, Characters, and Players are set up. There may be a simple script used as an example there.