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

Why am I receiving "attempt to index nil with 'Name'" from my script?

Asked by 3 years ago

I am receiving "attempt to index nil with 'Name'" from line 26 of my script. I've made several revisions in an attempt to try to fix the problem but now I'm completely stumped.

The script is located within a folder that contains:

Folder ("Hair") -> 3 accessories are within

IntValue ("Number")

LocalScript ("HairScript")

ImageButton ("LeftArrow")

ImageButton ("RightArrow")

TextButton ("InfoBox")

If any additional clarification is needed, please don't hesitate to ask me. All help is appreciated!

01local leftArrow = script.Parent.LeftArrow
02local rightArrow = script.Parent.RightArrow
03local midBox = script.Parent.InfoBox
04local numb = script.Parent.Number
05local character = script.Parent.Parent.CharacterViewer.Character
06 
07local hairFolder = script.Parent.Hair:GetChildren()
08 
09leftArrow.MouseButton1Click:Connect(function()
10    if numb.Value == 1 then
11        numb.Value = #hairFolder
12    else
13        numb.Value = numb.Value-1
14    end
15end)
View all 31 lines...
0
When does this error happen? Also, could you put a print(numb.Value) in between line 25 and 26 and test it? This will make debugging easier. movementkeys 163 — 3y
0
The error happens at startup since updateMidBox() is called to happen as the game starts ChirpPerson 74 — 3y

2 answers

Log in to vote
1
Answered by 3 years ago

Correct me if I'm wrong, but I think that the issue is that numb.Value is equal to zero. Lua arrays start at 1 so you can fix this by adding :

1numb.Value = 1
2--or--
3numb.Value = #hairFolder

at the top of your script once numb is defined.

Ad
Log in to vote
0
Answered by
Mineloxer 187
3 years ago

I am not entirely sure if this is the cause of the issue, but in many cases scripts load and run before game objects get a chance to load as well. This means that when scripts try and access these objects, they find nothing, and end up with "nil". (I noticed you said that this occurs at startup and this is why I think it's most likely this)

Luckily, easiest solution is to use :WaitForChild(). It takes the name of the instance you want to wait for, and an optional second argument that indicates the number of seconds to wait before giving up the wait. If you don't specify a second argument, it waits forever.

For example:

1-- This will wait for the instance "Number" till it loads
2local numb = script.Parent:WaitForChild("Number")

I recommend going through your scripts where you think the issue might happen, and using it.

Here is the API reference for it, if you want to find out more about it: https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

Answer this question