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

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

Asked by 6 years ago
Edited 6 years ago
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!

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) justoboy13 153 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

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
0
Thanks for the input, its working fine now. scourge495 2 — 6y
0
no probs greatneil80 2647 — 6y
0
if you are ever using game.Players.LocalPlayer, make sure it is in a localscript greatneil80 2647 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question