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
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")
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.
Just use LocalPlayer.Character.Humanoid instead of saving it in a variable. Works perfectly fine although lengthy.