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

I got a "Attempt to index nil with FindFirstChild" error on my dialog script, can someone help?

Asked by 3 years ago
Edited 3 years ago

--I keep getting this error and I don't get what's happening. All I know is that its in this line of code.

`
local HumanoidRootPart = NPC:FindFirstChild("HumanoidRootPart")  

**local ProximityPrompt = HumanoidRootPart:FindFirstChild("ProximityPrompt")**

if HumanoidRootPart and ProximityPrompt then                        ProximityPrompt.ObjectText  = NPC.Name 
ProximityPrompt.ActionText = "Talk to".. NPC.Name `

--script surrounded with '**' is the one not working.

0
Is NPC referenced anywhere in the script? JesseSong 3916 — 3y
0
Yes. Guest_F4ke 5 — 3y
0
Csn someone please help Guest_F4ke 5 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Usually "Attempt to index nil" happens if something hasn't loaded in yet, Maybe try changing

:FindFirstChild()

to

:WaitForChild()

WaitForChild should be self-explanatory, It waits for a child with said name to load in. FindFirstChild will find the first child with said name, if there's none. It will return "Nil"

I also realised you've not told the script what "NPC" refers to, so that also may be why it's returning nil.

Try making a variable for that as well.

local NPC = game.Workspace:WaitForChild("  ") --Insert NPC Name here.
local HumanoidRootPart = NPC:WaitForChild("HumanoidRootPart")
local ProximityPrompt = HumanoidRootPart:WaitForChild("ProximityPrompt")

Hopefully one of these answers helped.

0
Said "Infinite yeild possible on Local NPC = game.Workspace:WaitForChild("SusMan") Guest_F4ke 5 — 3y
0
Is there an NPC named SusMan? AwesomeGamer2223 105 — 3y
Ad

Answer this question