Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is anyone able to tell me why I am not able to find this players Humanoid Child?

local plr = game.Players.LocalPlayer
local dispHealth = script.Parent
local numHealth = dispHealth.Parent.healthNumber
local human = workspace:FindFirstChild(plr.Name).Humanoid

dispHealth.Size.X.Offset = human.Health * 2

Error message: Players.scourge495.PlayerGui.ScreenGui.healthBG.healthBar.Script:4: attempt to index a nil value

I am fetching the local Player that this script is attached to in line 1, and then i use it as a reference to try and fetch the player models humanoid.

I have attempted it without the additional '.Humanoid' and it finds the players model just fine. Also I have tried :FindFirstChild("Humanoid") whilst creating a new variable in the process but the same error message occurs when it attempts to extract a humanoid.

I'm a bit new to scripting but not programming, so simple slipups are still probably happening under my nose. If anyone would be as kind to tell me what aspect I'm missing i'd greatly appreciate it. Thanks!

1 comment

0

You are using .LocalPlayer in a Script. .LocalPlayer can only be called from a LocalScript (which is why workspace:FindFirstChild(plr.Name) returns nil)

2 answers

✓ Accepted answer

In a localscript in starterGui, you should do this:

repeat wait() until game:GetService("Players").LocalPlayer
local plr = game.Players.LocalPlayer
local numHealth = script.Parent.Parent.healthNumber
local human = game.workspace:FindFirstChild(plr.Name)

dispHealth.Size.X.Offset = human.Health * 2

3 comments

0

Thanks for the input, its working fine now.

0

no probs

0

if you are ever using game.Players.LocalPlayer, make sure it is in a localscript

Is this a local script?. Im assuming it is not due to the error line saying .script and not .LocalScript But if you are still doing a normal script just do plr = script.Parent.Parent.Parent.Parent.Parent humanoid = game.Workspace[plr.Name] If it is a local script i think the issue might be ur using () and not [] on line 4

This discussion is preserved as it was. New answers, comments, and votes are closed.