So I sometimes get an error in my main script which breaks my game. This error usually is “HumanoidRootPart is nil” or something along those lines. The error mostly occurs in parts of my script such as this one:
for Number, player in pairs(LobbyTeam:GetPlayers()) do
if player then
local Character = player.Character
player.Team = PlayingTeam
if Character.HumanoidRootPart then
Character.HumanoidRootPart.CFrame = GameTp.CFrame + Vector3.new(0,5,0)
end
end
end
This is supposed to teleport all players in the “LobbyTeam” to the “GameTp” and set their team to “PlayingTeam” (these variables are all defined earlier in the script). This usually works except on some rare occasions where it breaks the game.
Note: I’m not 100% certain what causes this but I think it happens when a player leaves the game at the wrong time.
If anyone knows how to prevent this, please let me know.
Thanks in advance!
You could try using FindFirstChild to find if the humanoidrootpart is there
for Number, player in pairs(LobbyTeam:GetPlayers()) do
if player then
local Character = player.Character
player.Team = PlayingTeam
local root = Character:FindFirstChild("HumanoidRootPart")
if root then
root.CFrame = GameTp.CFrame + Vector3.new(0,5,0)
end
end
end
If FindFirstChild does not find anything called HumanoidRootPart i nthe character, then it will return nil, so the next bit of code will only run if there’s a humanoidrootpart
It hopefully shouldn’t error since if it finds HumanoidRootPart, then root will contain it, and if it doesn’t find it, then root will be nil, and the way the if statement is made will only make the next code happen if root has an instance in it
The way you did it will always error because you’re trying to get a thing that may not be in the Character when the code is ran, which errors because that thing doesn’t exist in the character. It’s a simple mistake, you’re still learning!
for Number, player in pairs(LobbyTeam:GetPlayers()) do
if player then
local Character = player.Character
player.Team = PlayingTeam
if Character and Character.HumanoidRootPart then
Character.HumanoidRootPart.CFrame = GameTp.CFrame + Vector3.new(0,5,0)
end
end
end
There, you must make sure first that the Character is not nil, then check for the HumanoidRootPart. In your case, the player is loaded and you are checking for their HumanoidRootPart when their character is not loaded, it’s like you did nil.HumanoidRootPart.
Their issue wasn’t because the character was not loaded, their issue was that the HumanoidRootPart wasn’t found, which was returning nil for it and erroring. The issue was eventually discovered to be that OP had to use FindFirstChild instead of the method they had used which was erroring because they were referencing something in the Character that didn’t exist (HumanoidRootPart in this case)
But you know, when the character is not loaded, the HumanoidRootPart is also not loaded. The character and the HumanoidRootPart load at the same time.
And still, nil:FindFirstChild('HumanoidRootPart') wouldn’t be great use.
Let’s say we have 2 players in game with their character loaded. A player joins and the loop starts, since their character has not loaded yet, player.Character is nil. Doing nil:FindFirstChild would error.
Now that I think about it, you are right about the Character having a chance to error because of the Character not being loaded, but I don’t believe it’s an issue with OPs case judging by how the only error he kept getting was about the RootPart being nil, not sure if when the code runs would determine the frequency of the error.
If needed for @DoudGeorges, he could either wait for the character to exist before running, or just ignore the rest of the code for that player if their character is nil