Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make it so that it doesn't break if there is no "HumanoidRootPart"?

Asked by
ElBamino 153
3 years ago
Edited 3 years ago

Hey scriptinghelpers, I'm working on a minigame type of game. I'm having a problem when teleporting the players. Sometimes like if they reset, fall off the map, or whatever it is if they die when it tries to teleport them it breaks the game. I was wondering how I could fix this. Below you will find part of my code, I would provide the whole thing but, it's really long and you don't need it all so it will only be the teleporting part.

Edit: I changed the script to the solution.

This is a Script inside of ServerScriptStorage.

local function resetPlayers()
    game.Workspace.TP.NumberOne.Value = ""
    game.Workspace.TP.BoolValue.Value = false
    for _, player in pairs (game.Players:GetChildren()) do
        if player.Character:FindFirstChild("HumanoidRootPart") then
            player.Character.HumanoidRootPart.CFrame = CFrame.new(lobby)
        else
        end
    end
end
local function teleportPlayers()
    for _, player in pairs (game.Players:GetChildren()) do
        if player.Character:FindFirstChild("HumanoidRootPart") then
            player.Character.HumanoidRootPart.CFrame = CFrame.new(obby)
        else
        end
    end
end

2 answers

Log in to vote
1
Answered by 3 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

You can add a if statement to check for the humnaoidrootpart.

if player.Character:FindFirstChild("HumanoidRootPart") then
    --teleport code
end
0
Alright cool thanks, I appreciate it. I used an if then statement and else to continue my loop if not. I edited my answer to show. ElBamino 153 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
for _, player in pairs (game.Players:GetChildren()) do
    if player.Character:FindFirstChild("HumanoidRootPart") then
        player.Character.HumanoidRootPart.CFrame = CFrame.new(obby)
    end
end

Answer this question