This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
pairak's profile picture

attempt to concatenate nil and string

Started by pairak, 12 July 2016 - 09:35 AM
pairak #1
Posted 12 July 2016 - 11:35 AM
Ok, first off, im a n00b at this, but for a long time ive wanted to have a screen to monitor my induction Matrix from Mekanism. I couldnt find any scripts that worked so I figgured id write my own. I found a youtube guide, followed it, and then added a few new things based on what I learned.
This is what I want my script to do:
1: Show % of power stored / max power storable.
2: Show total power stored
3: Show power input / max input
4: show power output /max output

I was able to get this to work but then I noticed that CC think mekanism on my server use J instead of RF so it converted it, making all values 2,5X higher then they were supposed to, so I added new parameters for converting this, and while I was at it, divided by 1000 to show Krf instead of RF. Now for the Stored power, this works fine, but on the input/output lines I get the error in the title. The code is the same so I cant figgure this out, please help =(


power = peripheral.wrap("Induction Matrix_4")
mon = peripheral.wrap("monitor_8")

local maxPower = 0
local curPower = 0
local perPower = 0
local outPower = 0
local inpPower = 0
local capPower = 0
monX,monY = mon.getSize()

function checkPower()
  maxPower = power.getMaxEnergy()
  curPower = power.getEnergy()
  perPower = math.floor(((curPower/maxPower)*100)+0.5)
  currentPower = math.floor(((curPower/2.5)/1000000)+0.5)

  outPower = power.getOutput()
  inpPower = power.getInput()
  capPower = power.getTransferCap()
  outputPower = math.floor(((outPower/2.5)/1000)+0.5)
  inputPower = math.floor(((inpPower/2.5)/1000)+0.5)
  cappePower = math.floor(((capPower/2.5)/1000)+0.5)
end


function writeMon()
  mon.setBackgroundColor(colors.black)
  mon.clear()
  mon.setCursorPos(1,1)
--  mon.write("Power")
  title = "  Power:" .. perPower .. "% Full  "
  centerT(title, 1, colors.lightGray, colors.blue)
  title = "  Stored Power:" .. currentPower .."Mrf "
  centerT(title, 3, colors.lightGray, colors.blue)
  title = "  Input:" .. inputPower .. "/" .. cappedPower .. "Krf"
  centerT(title, 5, colors.lightGray, colors.blue)
  title = "  Output:" .. outputPower .. "/" .. cappedPower .. "Krf"
  centerT(title, 7, colors.lightGray, colors.blue)

end

function centerT(text,line,backColor,txtColor)
  mon.setBackgroundColor(backColor)
  mon.setTextColor(txtColor)
  length = (string.len(text))
  dif= math.floor(monX-length)
  x = math.floor(dif/2)
  mon.setCursorPos(x+1, line)
  mon.write(text)
end

function drawBar()
  bar = math.floor(((curPower/maxPower)*(monX-2))+0.5)
  mon.setCursorPos(2,monY-4)
  mon.setBackgroundColor(colors.red)
  mon.write(string.rep(" ", monX-2))

end

while true do
checkPower()
writeMon()
drawBar()

print(curPower .. "/" .. maxPower)
sleep(1)
end

Also, is there a way to have a comma set into the values? so instead of showing 230083 it shows 230,083? And if so, any ideas how to incorporate this into my code?

Thanks in advance for the assistance.


Also, heres the pastebin: http://pastebin.com/2Y595f4r
LBPHacker #2
Posted 12 July 2016 - 02:22 PM
Well, you didn't exactly tell us on which line the error occurs, but I'm going to assume it's line 36, where you try to concatenate cappedPower with constant strings. A couple of lines above you made the mistake of typing cappePower instead of cappedPower, thus leaving cappedPower undefined, or nil.

EDIT: Oh, forgot the second part. You could try this to insert commas:
local function insertCommas(number)
    local result = {}
    for chars in tostring(number):reverse():gmatch("..?.?") do
        table.insert(result, chars)
    end
    return table.concat(result, ","):reverse()
end
Edited on 12 July 2016 - 12:33 PM
pairak #3
Posted 12 July 2016 - 02:27 PM
…. I spent over a hour looking at this script, desperately trying to make sure there was no stupid errors in it, and there it was, a missing "d". Thank you so much for the help as now it works perfectly =D. I'm gonna go hit my head into some hard objects now, but once again, thank you =)

Also, tried that Comma code you posted, got this Error. "Bios:14 [String ".temp"]:13: unexpected symbol".
Line 14 is " for chars in tostring(number):reverse():gmatch("..?.?") do"
Bomb Bloke #4
Posted 13 July 2016 - 01:15 AM
Looks ok to me. Double-check for typos in the code you're actually running.
pairak #5
Posted 13 July 2016 - 07:06 AM
Ok, got the code in and no errors this time, but it didnt change anything or add the commas, was there something in the code I was supposed to change for it to work?
LBPHacker #6
Posted 13 July 2016 - 08:26 AM
The message is blaming line 13, not line 14. So, yeah, you'd better take a look at that line instead. Also, I'm pretty sure there are more words following unexpected symbol. Those are there to help you and, well, us, so if you're still having a hard time fixing it, it might be a good idea to post the full error message. If I had to guess, I'd say you typed something like
local result = {]