Chips
Chips

Reputation: 1

attempt to index a nil value (global 'random') lua problem

Hi I am new to lua and i was watching this tutorial about it and the person showed me this code

io.write("Your cash is: ",random.math(1,100))

I tried to do it on my online compiler and the error above showed up I think it was because he was using an older version of lua because it was uploaded in 2015, so can anyone help me with this problem?

Upvotes: 0

Views: 897

Answers (2)

Francisco
Francisco

Reputation: 450

Lua by default doesn't have a random library, this is why random is returning nil. You are probably looking for math.random:

io.write("Your cash is: ", math.random(1,100)) -- this is valid lua

Upvotes: 2

user10133166
user10133166

Reputation:

Whatever online compiler you are using doesn't have random library or you've accidentally overwritten the variable somewhere.

You can write print( random ) to the very top of your code. If it says nil, then you don't have the random library.

Upvotes: 0

Related Questions