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

Attempt to index a nil value meaning?

I really don't understand what indexing is in lua. If anyone could tell me this would probably solve most of my problems. Basically I tried to make a script where the Worker1Cost value would be displayed on a text label. But I get the error Attempt to Index a Nil value. I would appreciate if you would explain what the error actually means.

while wait() do

local player = game.Players.LocalPlayer

script.Parent.Text = player:WaitForChild("gen"):FindFirstChild("Worker1Cost").Value .. " Robux"

end

2 comments

0

Indexing means getting a property or method of an object. Also, what are you doing with "Value .. ' Robux'"? That's probably why you're getting the error.

0

I fixed this but I wanted the text label to display the value then robux after it

1 answer

✓ Accepted answer

What is indexing a value?

local t = {1, 2, 3}
print(t[3]) --index with 3
print(t["asdf"]) --index with "asdf"
print(t.asdf) --equivalent to above line (just a different way)

That is indexing. Tables can be indexed with numbers, strings, bools, etc. (any non nil value). nil can't be indexed hence the error. You haven't specified where you got the error but the error is probably happening because FindFirstChild("Worker1Cost") is returning nil and you are indexing it.

Because you used FindFirstChild(), I assume that you want to get the value of it only if it exists. You can use an if statement before hand to make sure it isn't nil.

while true do
	wait()
	local player = game.Players.LocalPlayer
	local worker1Cost = player:WaitForChild("gen"):FindFirstChild("Worker1Cost")
	if worker1Cost then
		script.Parent.Text = worker1Cost.Value
	end
end

1 comment

0

So indexing is referencing a value in a table?

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