Hi Everyone,
While checking my Error Reports I noticed the following error that has occurred with a player when locating their HumanoidRootPart.
However, I’m not sure why this was caused and would appreciate any feedback, my code is below.
Was it caused by the fact that I did not use FindFirstChild? The username in the error report is clearly the user’s character model, so the script did locate the correct character.
No error was displayed when it came to getting the Humanoid, which leads me to believe I should have used FindFirstChild.
Code:
function rewardWinner(winnerName)
local winner = Players:FindFirstChild(winnerName)
local winnerChar = winner.Character
local humanoid = winnerChar:FindFirstChildOfClass("Humanoid")
local HRP = winnerChar.HumanoidRootPart
HRP.Position = Vector3.new(10, 3.75, -125)
end
FindFirstChild doesn’t have much to do with it. The method returns an Instance with the given name if it can find it; if it can’t, it will return nil
. In your script, the value of humanoid
isn’t being used, so there’s no possible chance it could cause an error.
Your function should look like this:
function rewardWinner(winnerName)
local winner = Players:FindFirstChild(winnerName)
local winnerChar = winner.Character
local humanoid = winnerChar:FindFirstChildOfClass("Humanoid")
local HRP = winnerChar:FindFirstChild("HumanoidRootPart")
if HRP then -- Checks if the player has a HumanoidRootPart first before trying to assign its position
HRP.Position = Vector3.new(10, 3.75, -125)
end
end
Correct me if I’m wrong but R6 has no HRP, is this R6? I would recommend using char.PrimaryPart to be safe!
R6 has a HumanoidRootPart
Ok thank you, apparently they weren’t always in R6 models that’s probably why I though that.
It is R6, but as the first guy mentioned R6 also has HumanoidRootPart.
This is a rare error though, I have never encountered it before so it might be something on Roblox’s end with latency.
However, I might try the method with PrimaryPart, it seems like it would avoid the error as the character will always have a PrimaryPart.
1 Like