-4

I am trying to make the computer check how much power Energy cell has, but when i run this program it says: attemt to index ? (a nil value) Any suggestions?

local energyCell = peripheral.wrap("back")
local energyCellMax = energyCell.getMaxEnergyStored("top")
local productionTime = 250
local energyPerTick = 320
local energyCreated = productionTime * energyPerTick * 20
local energyThreshold = energyCellMax - energyCreated
local dropperSide = "top"

local function dropperPulse()
  redstone.setOutput(dropperSide, true)
  sleep(1)
  redstone.setOutput(dropperSide, false)
  sleep(productionTime)
end

while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Current Charge Level:")
  print("    "..math.floor(100*energyCell.getEnergyStored("top")/energyCellMax).."%")
  if (energyCell.getEnergyStored("top") < energyThreshold) then
    print("Charging...")
    dropperPulse()
  end
  sleep(5)
end

1 Answers1

0

The only problem i can see with your code that would allow for that error, is if you cannot wrap the peripheral. Make sure that it's actually connected on the back of the computer.

If you're connected to the energy cell by wired modem, when you right click on the modem attached to the energy cell you should get a message like "Connected thermalexpansion_energycell" or whatever, and that thermal expansion name, the whole thing, should be put inside the peripheral.wrap.

Once you do that however you want to put in the energyCell.getEnergyStored and energyCell.getMaxEnergyStored "unknown" instead of "top"

Flamanis
  • 156
  • 5