This is my script that keeps giving me the error this error 09:30:35.247 - Workspace.protectiverobos.Up.LocalScript:3: attempt to call method 'FindFirstChild' (a nil value)
01 | local player = game.Players.LocalPlayer |
02 | local name = game.Players.LocalPlayer.Name |
03 | local gethumanoid = game.workspace.name:FindFirstChild( "Humanoid" ) |
04 |
05 | while true do |
06 | wait() |
07 | if gethumanoid.Health < = game.ServerStorage.PlayerStats.NeededHealth then |
08 | script.Parent.Value = false |
09 | end |
10 | end |
You can get the player's Character from Player.Character and from there you can either use char.Humanoid
or char:FindFirstChild("Humanoid")
or char:WaitForChild("Humanoid") which I used below.
EDIT: For your new error, I have created a new local variable to get the ServerStorage
service and wait for PlayerStats
to load in.
01 | local player = game.Players.LocalPlayer |
02 | local char = player.Character -- replaced 'name' variable |
03 | local gethumanoid = char:WaitForChild( "Humanoid" ) -- WaitForChild is used to prevent any errors |
04 | local plrStats = game:GetService( "ServerStorage" ):WaitForChild( "PlayerStats" ) -- Created this variable to wait for the PlayerStats to load in to (hopefully) prevent any errors |
05 |
06 | while true do |
07 | wait() |
08 | if gethumanoid.Health < = plrStats.NeededHealth then |
09 | script.Parent.Value = false |
10 | end |
11 | end |
You're accessing the humanoid wrong. You're trying to find it within the workspace name, this is incorrect. Try this instead:
1 | local player = game.Players.LocalPlayer |
2 | local humanoid = player.Character:FindFirstChild( 'Humanoid' ) |