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

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

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!

local leftArrow = script.Parent.LeftArrow
local rightArrow = script.Parent.RightArrow
local midBox = script.Parent.InfoBox
local numb = script.Parent.Number
local character = script.Parent.Parent.CharacterViewer.Character

local hairFolder = script.Parent.Hair:GetChildren()

leftArrow.MouseButton1Click:Connect(function()
	if numb.Value == 1 then
		numb.Value = #hairFolder
	else
		numb.Value = numb.Value-1
	end
end)

rightArrow.MouseButton1Click:Connect(function()
	if numb.Value == #hairFolder then
		numb.Value = 1
	else
		numb.Value = numb.Value+1
	end
end)

function updateMidBox()
	midBox.Text = ("Hair: "..hairFolder[numb.Value].Name)
	--local hair = hairFolder[numb.Value]:Clone()
	--hair.Parent = character
end

updateMidBox()

2 comments

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.

0

The error happens at startup since updateMidBox() is called to happen as the game starts

2 answers

✓ Accepted answer

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 :

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

at the top of your script once numb is defined.

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:

-- This will wait for the instance "Number" till it loads
local 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

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