How do I make this respawn script only respawn if the player still exists

So I have this pvp script where it respawns the player then teleports them. I have them respawn because if they are sitting in a seat in the lobby they don’t teleport for some reason. And so when the person who lives the pvp wins, I have the player respawn. Well sometimes when I respawn them at the beginning or end of the match they left, causing the script to break and error saying:
image
I even use if statements to test if the player’s character is still there, but that doesn’t seem to help. I have thought of using a call on the function but I feel it might be unnecessary because this problem may be fixable

Heres the code:

-- Sorry its so long
local ServerStorage = game:GetService("ServerStorage")
local PlayersService = game:GetService("Players")
local ChatMessageFunction = game:GetService("ReplicatedStorage"):WaitForChild("SytemMessage")
function SystemMessage(Text,Color)
	local SystemMessageEvent = game
	if not Text or not Color then return end
	if not typeof(Text) == "string" and not typeof(Color3) == "Color3" then return end
	ChatMessageFunction:FireAllClients(Text,Color)
end
function RunGame()
	local Maps = ServerStorage:WaitForChild("Maps"):GetChildren()
	local Gears = ServerStorage:WaitForChild("Gear"):GetChildren()
	if ServerStorage and Maps and Gears then
		local MapNumber = math.random(1,#Maps)
		local GearNumber = math.random(1,#Gears)
		local PlayersList = PlayersService:GetPlayers()
		local Players = {}
		for _, Player in pairs(PlayersList) do
			if Player:FindFirstChild("IsLoaded") then
				if Player.IsLoaded.Value then
					table.insert(Players, #Players + 1, Player)
				end
			end
		end 
		if #Players < 2 then SystemMessage("Not Enough People To Start Match, Canceling Match!",Color3.fromRGB(255,255,0)) return end
		local Map = Maps[MapNumber]
		local AvailableSpawns = Map.Spawns:GetChildren()
		local Gear = Gears[GearNumber]
		local MapName = Map.Name
		local GearName = Gear.Name
		local PVPPLR_NO1 = math.random(1,#Players)
		local PVP_PLAYER_1 = Players[PVPPLR_NO1]
		table.remove(Players,PVPPLR_NO1)
		local PVPPLR_NO2 = math.random(1,#Players)
		local PVP_PLAYER_2 = Players[PVPPLR_NO2]
		table.remove(Players,PVPPLR_NO2)
		if not PVP_PLAYER_1 or not PVP_PLAYER_2 then SystemMessage("Error While Setting Up Match, Match Cancelled!", Color3.fromRGB(255,0,0)) return end
		SystemMessage("Starting Match, The Selected Players To Fight Are: "..PVP_PLAYER_1.Name.." And "..PVP_PLAYER_2.Name..", The Selected Map Will Be: "..MapName..", And The Selected Gear Will Be: "..GearName..".",Color3.new(1,1,1))
		Map.Parent = workspace.CurrentMap
		local PVP_SPAWN_NO1 = math.random(1,#AvailableSpawns)
		local PVP_SPAWN_1 = AvailableSpawns[PVP_SPAWN_NO1]
		table.remove(AvailableSpawns,PVP_SPAWN_NO1)
		local PVP_SPAWN_NO2 = math.random(1,#AvailableSpawns)
		local PVP_SPAWN_2 = AvailableSpawns[PVP_SPAWN_NO2]
		wait(5)
		PVP_PLAYER_1:LoadCharacter()
		PVP_PLAYER_2:LoadCharacter()
		local PVP_CHAR_1 = PVP_PLAYER_1.Character or PVP_PLAYER_1.CharacterAdded:Wait()
		local PVP_CHAR_2 = PVP_PLAYER_2.Character or PVP_PLAYER_2.CharacterAdded:Wait()
		if not PVP_CHAR_1 or not PVP_CHAR_1:FindFirstChild("Humanoid") or not PVP_CHAR_2 or not PVP_CHAR_2:FindFirstChild("Humanoid") then SystemMessage("Error While Setting Up Match, Match Cancelled!", Color3.fromRGB(255,0,0)) return end

		PVP_CHAR_1.Humanoid.WalkSpeed = 0
		PVP_CHAR_2.Humanoid.WalkSpeed = 0
		PVP_CHAR_1:SetPrimaryPartCFrame(PVP_SPAWN_1.T.CFrame)
		PVP_CHAR_2:SetPrimaryPartCFrame(PVP_SPAWN_2.T.CFrame)
		PVP_PLAYER_1:WaitForChild("InGame").Value = true
		PVP_PLAYER_2:WaitForChild("InGame")
		wait(5)
		local PVP_GEAR_1 = Gear:Clone()
		local PVP_GEAR_2 = Gear:Clone()
		PVP_CHAR_1.Humanoid.WalkSpeed = 25
		PVP_CHAR_2.Humanoid.WalkSpeed = 25
		PVP_GEAR_1.Parent = PVP_CHAR_1
		PVP_GEAR_2.Parent = PVP_CHAR_2
		local PVP_WINNER
		while true do
			if PVP_CHAR_2.Parent ~= game.Workspace or PVP_CHAR_2.Humanoid.Health == 0 or PVP_CHAR_1.Parent ~= game.Workspace or PVP_CHAR_1.Humanoid.Health == 0 then
				break
			end
			wait()
		end
		if PVP_CHAR_1.Parent == workspace and PVP_CHAR_1.Humanoid.Health ~= 0 then
			PVP_WINNER = PVP_PLAYER_1
		elseif PVP_CHAR_2.Parent == workspace and PVP_CHAR_2.Humanoid.Health ~= 0 then
			PVP_WINNER = PVP_PLAYER_2
		else
			PVP_WINNER = "No one"
		end
		if typeof(PVP_WINNER) ~= "string" then
			PVP_WINNER.leaderstats.KOs.Value = PVP_WINNER.leaderstats.KOs.Value + 1
			SystemMessage(PVP_WINNER.Name.." Won The Match!",Color3.fromRGB(0,255,0))
			wait(5)
			PVP_WINNER:WaitForChild("InGame").Value = false
			PVP_WINNER:LoadCharacter()
			Map.Parent = ServerStorage.Maps
		end
	end
end
while true do
	RunGame()
	wait(10)
end

An additional check of Player.Parent ~= nil should be applicable in this case. If the player has left, the player object is discarded from the game.

1 Like

Ok thanks, This will probably fix the problem now that I think of it. Thanks!