Reputation: 771
I'm very new to Lua and I'm trying to globally initialise a table at the very start of my program. At the top, I have:
storage = {}
Then, I want to iterate over elements in this table inside functions in the same file. One example is:
local output
for item in storage do
output = output .. item
end
return output
In this case, I get:
attempt to call a nil value
On the line beginning with for
.
I have also tried printing out storage[1]
. In this case I get:
attempt to index local 'storage' (a nil value)
Could someone please explain in simple terms what could be wrong here?
Upvotes: 1
Views: 106
Reputation: 26754
You are not showing the entire script, but it's clear that storage
value gets reset somewhere between your initialization and using in for item in storage do
, because if it keeps the value, you'd get a different error: attempt to call a table value
.
You need to use ipairs
or pairs
function in the loop -- for key, item in pairs(storage) do
-- but you first need to fix whatever resets the value of storage
.
Upvotes: 1