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

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

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

✓ Accepted answer

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

4 comments

0

Now it says this

0

15:02:26.270 - PlayerStats is not a valid member of ServerStorage

0

My answer has been edited.

0

Now it gives me no errors, but doesn't work.

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') 

2 comments

0

I believe 'character' is capitalized to be 'Character' and why did you include the name variable if you didn't use it?

0

the name variable was in the original. I assumed he wanted it there for a reason. I will change the character thing now.

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