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?
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)
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.
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)