Reputation: 49
So I have an array of people that I want to choose from at random...
local strong = {
'Laura',
'David',
'Christopher'
}
How would I pick randomly from this table? So far I have ran
print(math.random(#strong))
But when I run that get this error
lua: again.lua:19: attempt to get length of global 'strong' (a nil value)
Why would the array be nil, even though there is something inside the array?
Upvotes: 1
Views: 199
Reputation: 3225
If you use Lua interactively, put everything inside a do
block, or remove local
from your table variable to make it global. The interpreter executes each statement separately, losing the local
of the previous statement.
Also, set the random seed with something like: math.randomseed(os.time())
to avoid getting the same value each run.
Upvotes: 1