How to detect if the player is still in the game

Im trying to detect if the player is still in the game to prevent an error.

Script:

local prox = script.Parent

prox.Triggered:Connect(function (player)
if player.Character:FindFirstChild("HumanoidRootPart”) then
local HRP = player.Character:FindFirstChild("HumanoidRootPart”)
HRP.CFrame = workspace.Part.CFrame
wait(5)--if player leaves the game before the wait() 
HRP.CFrame = workspace["Part2"].CFrame --This line of code could throw an error
end

How do i prevent an error, if the player leaves the game before the 5 seconds?

1 Like

Does the error affect the game itself? If it doesn’t I wouldn’t be worried about this.

Here’s a quick example of utilizing :FindFirstChild to check if there is a player with a specified name. You should be able to implement this nicely into your code.

game.Players.PlayerAdded:Connect(function(plr)
	local playersName = plr.Name
	task.wait(5)
	if game.Players:FindFirstChild(playersName) then
		-- player is still in game
	end
end)

Yes the proximity prompt would stop working after the error

Ive already identified the player when the proximity prompt is triggered along with the HumanoidRootPart

The problem is that an error occurs if the player leaves the game… Something like attempt to nil index with “HumanoidRootPart”

I’m not saying to use a PlayerAdded event, I was showcasing the usage of detecting if the player is still in the game to put you on the right path.

You’ll have to use the same method of detecting if the player still actually exists. Here is an example of how you’d use an if statement to detect it explicitly using your code.

local prox = script.Parent

prox.Triggered:Connect(function(plr)
	if plr.Character:FindFirstChild("HumanoidRootPart") then
		local HRP = plr.Character.HumanoidRootPart
		task.wait(5)
		if game.Players:FindFirstChild(plr.Name) then 
			print("the player is still in game.")
           -- your teleport code would go here
		else
			print("the player is no longer in game.")
		end
	end
end)

Keep in mind this won’t account for other instances where the HumanoidRootPart no longer exists, this is simply as the question stated to detect if the player is still actually in game. It may be better to add a secondary check to determine if the HRP actually still exists as there are other instances, such as when the character is removed during a client resetting that the player would still exist but the character would not.

1 Like
local Script = script
local Prompt = Script.Parent

local function OnTriggered(Player)
	local Character = Player.Character
	if not Character then return end
	task.wait(5)
	if not Character.Parent then return end
	Character:PivotTo(CFrame.new(0, 0, 0))
end

Prompt.Triggered:Connect(OnTriggered)
1 Like

This code shouldn’t error since the HumanoidRootPart doesn’t immediately get garbage collected (removed) when a Player leaves the game ( or dies ).

It does, when a player leaves the game or resets before the wait() time but i fixed it using

pcall()

1 Like