jjbbss
jjbbss

Reputation: 179

lua variable type nil even though i just assigned it

I have a part of a lua script here:

local cmp = require("component")
local r = cmp.br_reactor
local e = require("event")
local unicode = require ("unicode")
local exit = false

local buffersize = 10000000

local last_tick_percent = 1

print(type(last_tick_percent))

function stored_energy()
  local rf_stored = r.getEnergyStored()
  local rf_percent = (rf_stored/buffersize)*100
  print(type(rf_precent))
  print(type(last_tick_percent))
  local delta_percent = rf_percent - last_tick_percent
  last_tick_percent = re_percent
  return rf_percent.."% ["..rf_stored.."] | "..unicode.char(916)..": "..delta_percent.."%"
end

The first print is not even executed for some reason. Inside the function, the first print returns Number while the second print returns nil.

Now I am getting the error attempt to perform arithmetic on upvalue "last_tick_percent" ( a nil value), obviously because last_tick_percent is nil which the print(type(..)) showed.

But i just assigned it literally 5 lines above.

So the questions are:

  1. Why is last_tick_percent nil and how can i fix that?
  2. why is the first print not executed?

Upvotes: 1

Views: 775

Answers (1)

Mike Corcoran
Mike Corcoran

Reputation: 14564

you are assigning re_percent which is not declared in your script to last_tick_percent inside stored_energy. i am assuming you meant to assign rf_percent.

Upvotes: 1

Related Questions