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

Reviving an old script error: Attempting to index a nil value

Started by VinylTheUnicorn, 15 May 2015 - 03:15 PM
VinylTheUnicorn #1
Posted 15 May 2015 - 05:15 PM
hello, im currently reviving this old script i found for compatibility with 1.7.10 and its been going really well until this little snag.

local function colorPerc(num)
local num = tonumber(num)
if num then
if num > 90 then
term.setTextColor(colors.lime)
elseif num > 60 then
term.setTextColor(colors.yellow)
elseif num > 30 then
term.setTextColor(colors.orange)
else
term.setTextColor(colors.red)
end
monitor.write(num.."%")
space = #tostring(num) - 5 * (-1)
for i = 1, space do
monitor.write(" ")
end
term.setTextColor(colors.white)
end
end


at monitor.write(num.."%") i get thrown the error attempting to index a nil value and i can figure out for the life of me why. any help is appreciated thanks.
Cranium #2
Posted 15 May 2015 - 05:36 PM
It's because the monitor was never wrapped.
local monitor= peripheral.wrap(side)
flaghacker #3
Posted 15 May 2015 - 05:38 PM
And please upload your code to pastebin or put it in code tags in the future.

 code here 

without the space before the /.
Edited on 15 May 2015 - 03:43 PM
VinylTheUnicorn #4
Posted 15 May 2015 - 05:41 PM
sorry, i should have specified that the monitors are already warped in a section above this one.
Lyqyd #5
Posted 15 May 2015 - 05:44 PM
Please post the whole code. If it's too large to reasonable post on the forums, put it up on pastebin and post a link to it. We can't provide the best help if you don't post all of the code.
MKlegoman357 #6
Posted 15 May 2015 - 05:47 PM
Then that means the monitor doesn't exist under whatever side/name you are wrapping it. Make sure you're not mixing 'left' with 'right' or if the monitor is connected with a wired modem make sure the cable is connected to the monitor (half of the the texture turns red) and that the network name is correct.
VinylTheUnicorn #7
Posted 15 May 2015 - 06:23 PM
hello, sorry i had to go some where. i assume 221 lines is unreasonable for the forum so here is a paste bin link: http://pastebin.com/SZb2BUh0 also sorry about pasting the code in the forum without tags. im new to the whole forum thing. anyways thanks for the help guys.
Bomb Bloke #8
Posted 16 May 2015 - 12:25 AM
Difficult to address the problems that script has one-by-one. There are quite a few potential issues.

You do define a "monitor" variable on line 83, but it's local to the tiny little "for" loop started on line 82, and so is discarded after line 87. Read up on scope.

But let's say we delete lines 4 to 95 and replace them with something like this:

Spoiler
local cells = {}

local function addCells(...)
	for i = 1, #arg do  -- "arg" is a numerically indexed table containing everything passed to the function (wrapped peripherals, in this case).
		cells[#cells + 1] = arg[i]
	end
end

-- http://www.computercraft.info/wiki/Peripheral.find
addCells(peripheral.find("tile_thermalexpansion_cell"))  -- Wrap all available peripherals of type "tile_thermalexpansion_cell", pass the lot to addCells().
addCells(peripheral.find("powered_tile"))
addCells(peripheral.find("capacitor_bank"))

-- "cells" now contains all available peripherals of the above types, wrapped.

local monitors = {peripheral.find("monitor",
	function(name, object)  -- If this function returns false when passed a found monitor, it won't be included in peripheral.find's results.
		local xSize, ySize = object.getSize()
		return xSize > 38 and ySize > 18 and object.isColour()
	end)}
	
-- "monitors" now contains all available colour-capable monitor peripherals that're at least 39x19.
-- So we can do stuff like monitors[1].write("whatever"), monitors[2].setTextColour(colours.whatever), up to however many monitors we have...

-- Leaving just the turbines and reactors:
local turbines = {peripheral.find("BigReactors%-Turbine")}
local reactors = {peripheral.find("BigReactors%-Reactor")}

--Check for storage cells and monitors before continuing
if #cells == 0 then    -- #cells returns the number of entries in the cells table.
    print("No RF storage found. Exiting script!")
    return
end

if #monitors == 0 then
    print("No Monitor found. Exiting script!")
    return
end

--Print connected peripherals
print("Peripherals connected:")
if #monitors == 1 then print("1 Monitor") else print(#monitors.." Monitor") end
if #cells == 1 then print("1 Cell") else print(#cells.." Cells") end
if #turbines == 1 then print ("1 Turbine") else print (#turbines.." Turbines") end
if #reactors == 1 then print ("1 Reactor") else print (#reactors.." Reactors") end

The script has other issues, but hopefully this puts you on the right track of finding and fixing them.
Edited on 15 May 2015 - 10:29 PM
VinylTheUnicorn #9
Posted 16 May 2015 - 05:53 AM
thank you, that helped a ton. im pretty new to lua so im sure the code is a mess and rough around the edges but your revision is helping me fly through reviving the script!