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

This script using :FindFirstChild is giving me an error, I cant figure out why?

Asked by 5 years ago

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)

local player = game.Players.LocalPlayer
local name = game.Players.LocalPlayer.Name
local gethumanoid = game.workspace.name:FindFirstChild("Humanoid")

while true do
wait()
if gethumanoid.Health <= game.ServerStorage.PlayerStats.NeededHealth then
script.Parent.Value = false
end
end

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

local player = game.Players.LocalPlayer
local char = player.Character -- replaced 'name' variable
local gethumanoid = char:WaitForChild("Humanoid") -- WaitForChild is used to prevent any errors
local plrStats = game:GetService("ServerStorage"):WaitForChild("PlayerStats") -- Created this variable to wait for the PlayerStats to load in to (hopefully) prevent any errors

while true do
    wait()
    if gethumanoid.Health <= plrStats.NeededHealth then
        script.Parent.Value = false
    end
end
0
Now it says this zandefear4 90 — 5y
0
15:02:26.270 - PlayerStats is not a valid member of ServerStorage zandefear4 90 — 5y
0
My answer has been edited. BennyBoiOriginal 293 — 5y
0
Now it gives me no errors, but doesn't work. zandefear4 90 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited by User#24403 5 years ago

You're accessing the humanoid wrong. You're trying to find it within the workspace name, this is incorrect. Try this instead:

local player = game.Players.LocalPlayer
local humanoid = player.Character:FindFirstChild('Humanoid') 
0
I believe 'character' is capitalized to be 'Character' and why did you include the name variable if you didn't use it? BennyBoiOriginal 293 — 5y
0
the name variable was in the original. I assumed he wanted it there for a reason. I will change the character thing now. DreamInspired 61 — 5y

Answer this question