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

Attempt to index a nil value meaning?

Asked by 5 years ago

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
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. DeceptiveCaster 3761 — 5y
0
I fixed this but I wanted the text label to display the value then robux after it CarmiteStars 9 — 5y

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

What is indexing a value? lua 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.

lua 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

0
So indexing is referencing a value in a table? CarmiteStars 9 — 5y
Ad

Answer this question