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

Attempt to index nil with 'FindFirstChild' ?

Asked by 3 years ago
Edited 3 years ago

Hello. In this script, I have included the following

local localplr = game.Players.LocalPlayer
local char = localplr.Character
local hum = char:FindFirstChild("Humanoid")

and got this error

15:21:04.792 - Players.Gmorcad12345.PlayerScripts.{NEW}WildPetEncounter:45: attempt to index nil with 'FindFirstChild'

I tried the same script but changed to WaitForChild and got the same error, just the 'findfirstchild' replaced with 'waitforchild'

how do i fix this problem? every character will have a humanoid so I don't see why this error occured. script is in a local script parented to starter player scripts

thx for ur time!!

[[EDIT]]

So at the bottom of the script, i had a Touched function. when i wrote

localplr.Character.Humanoid.WalkSpeed = 0
--in the touched function

no error occured.

(btw i commented out the line which caused an error)(line 3 of the previous script)

what could be the cause of the previous error then? im a 100% sure i spelt everything correctly

again, thanks for your time

0
replace "-" on line 3 with "=" WideSteal321 773 — 3y
0
oops, that was a typo. in the actual script i actually typed it correctly and it still doesnt work. Gmorcad12345 434 — 3y
0
Using the touched event on the StarterPlayerScripts is totally unnecessary. Just use a Server Script and use the touched event. Look at my answer below for more details. Dovydas1118 1495 — 3y
0
see my answer SANS24408 0 — 3y

3 answers

Log in to vote
0
Answered by
Mroczusek 111
3 years ago

The issue here is that the script doesn't find the object you're trying to use the :FindFirstChild() method.

Either the Character doesn't exist yet, or the humanoid hasn't loaded in. Let me show you what you should do in this situation (always do this if the script executes right after the player joins!)

local localplr = game.Players.LocalPlayer
local char = localplr.Character or localplr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This is the new answer after the edit.

Unlike the character, the humanoid does not replicate to the server automatically, so you will NEED to use a remote event. But that it is totally unnecessary. If you're using a touched event, you might as well use a Server Script and parent it to the part you wanna touch. Here's the script (comments included):

local part = script.Parent

part.Touched:Connect(function(hit) --This will get the part that hit this specific part. the hit parameter.
    if game.Players:GetPlayerFromCharacter(hit.Parent) then --Only use this if you wanna get the player ONLY. Otherwise use hit.Parent:FindFirstChild("Humanoid").
        hit.Parent.Humanoid.WalkSpeed = 0
    end
end)

Note: This doesn't account for debounces and stuff.

0
Well, the reason why I used a Local script is because I only want the functions inside the touched events to be local to the player, so others can't see it. Thanks btw Gmorcad12345 434 — 3y
Log in to vote
0
Answered by 3 years ago

Just use LocalPlayer.Character.Humanoid instead of saving it in a variable. Works perfectly fine although lengthy.

0
No. It doesn't work. It only works because the humanoid loaded it in already. In my case, the humanoid probably haven't loaded yet as stated in the answers above. Gmorcad12345 434 — 3y

Answer this question