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
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