Attempt to index nil

I made a custom hotbar a couple of weeks ago but today it just broke. I keep getting errors like the image returning as nil, etc.

Here is the code : (it worked like literally just yesterday)

while wait()  do
	for inedx, UI_ in pairs(script.Parent:GetChildren()) do
		if UI_:IsA("Frame") and tonumber(UI_.Name) then
			local image =script.libry:WaitForChild(UI_.Data.ToolName.Value).image.Value
			UI_.ImgIcon.Image =  tostring(image)
		else
			wait()
		end
	end
end

Any help is greatly appreciated!!
Have a good day and a Merry Christmas

1 Like

In which line of the code is the error?

1 Like

sorry, forgot to include that. it is line 4

1 Like

If this script worked previously, it’s probably because you moved something that the script needs to access. Is the error ‘Attempt to index nil with “Image”’ or is it something else?

lemme check again, ill let you know in a min

it says "attempt to index nil with ‘image’ " as in the StringValue “image” that it is looking for. the value is where it is supposed to be though

I tested the script in an empty game and was able to get it to work by creating objects based on what the script calls for. Here’s what my hierarchy looks like (the LocalScript is the script with your code):
Screen Shot 2021-12-21 at 12.57.38 PM

Always use :WaitForChild when indexing something in PlayerScripts (StarterPlayerScripts). LocalScripts will run before everything is loaded, meaning you’ll get unexpected results randomly like in your case.

i have a wait(5) before the script starts

Before the local image line add some prints:

print(UI_)
print(UI_.Data)
print(UI_.Data.ToolName)
print(UI_.Data.ToolName.Value)

then tell us the output result which is shown right before it errors.

2 Likes