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

I like some help with this

Started by robertgoogle, 12 August 2012 - 03:02 AM
robertgoogle #1
Posted 12 August 2012 - 05:02 AM
ok so I like to have a computer display if there's power or not on a tekkit server
win I run the program I get a error line 11 attempt to index ? (a nil value)

can some one help me find what is wrong

while true do
side = "right"
if redstone,getInput("left", "true")
then mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power NO")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end
Cranium #2
Posted 12 August 2012 - 05:05 AM
you forgot the "t" in mon.setTextScale(1)

edit: you also need to remove the quotations from redstone.getInput("right", true)
Pharap #3
Posted 12 August 2012 - 06:23 AM
ok so I like to have a computer display if there's power or not on a tekkit server
win I run the program I get a error line 11 attempt to index ? (a nil value)

can some one help me find what is wrong

while true do
side = "right"
if redstone,getInput("left", "true")
then mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power NO")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end

You only assign mon in the first condition, in the else condition, you are using mon before it exists.

If you format your code properly, it should look more like this:

while true do
side = "right"
if rs.getInput("left", "true") then
mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power ON")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end

here you can see that mon is being assigned after the if, and not in the else section, therefore mon does not exist in the else condition, thus: an error.
Just move the mon declaration outside of the if statement and it should work fine.
Cranium #4
Posted 12 August 2012 - 03:20 PM
ok so I like to have a computer display if there's power or not on a tekkit server
win I run the program I get a error line 11 attempt to index ? (a nil value)

can some one help me find what is wrong

while true do
side = "right"
if redstone,getInput("left", "true")
then mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power NO")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end

You only assign mon in the first condition, in the else condition, you are using mon before it exists.

If you format your code properly, it should look more like this:

while true do
side = "right"
if rs.getInput("left", "true") then
mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power ON")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end

here you can see that mon is being assigned after the if, and not in the else section, therefore mon does not exist in the else condition, thus: an error.
Just move the mon declaration outside of the if statement and it should work fine.
You still forgot some of the spelling corrections I suggested, Pharap. :P/>/>
Pharap #5
Posted 12 August 2012 - 03:23 PM
You only assign mon in the first condition, in the else condition, you are using mon before it exists.

If you format your code properly, it should look more like this:

while true do
side = "right"
if rs.getInput("left", "true") then
mon = peripheral,wrap(side, true)
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("power ON")
sleep(2.0)
mon.clear()
else
mon.setTexScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end

here you can see that mon is being assigned after the if, and not in the else section, therefore mon does not exist in the else condition, thus: an error.
Just move the mon declaration outside of the if statement and it should work fine.
You still forgot some of the spelling corrections I suggested, Pharap. :P/>/>

What was it I said was one of my most common coding mistakes again? lol

Yeah, text is one of the words that trips me up a lot (mostly in C# due to case sensitivity)
Cranium #6
Posted 12 August 2012 - 03:27 PM
Here's the corrected code for spelling and the mon being assigned too late.

while true do
local side = "right"
local mon = peripheral,wrap(side, true)
if rs.getInput("left", "true") then
mon.setTextScale(1)
mon.setCursorPos(1,1)
mon.write("power ON")
sleep(2.0)
mon.clear()
else
mon.setTextScale(1)
mon.setCursorPos(1,1)
mon.write("OUT OF POWER")
sleep(2.0)
mon.clear()
end
end
Pharap #7
Posted 12 August 2012 - 03:36 PM
Here's the corrected code for spelling and the mon being assigned too late.

Sorry, but my programming brain has just kicked into gear and I've spotted some improvements that could be made:


while true do
local side = "right"
local mon = peripheral,wrap(side, true)
mon.setTextScale(1)
mon.setCursorPos(1,1)
if rs.getInput("left", "true") then
mon.write("power ON")
else
mon.write("OUT OF POWER")
end
sleep(2.0)
mon.clear()
end

Condensed by 4 lines. I probably wouldn't have noticed that were it not for the repost.
Cranium #8
Posted 12 August 2012 - 03:43 PM
I see, it makes sense now! Good eye!
Pharap #9
Posted 12 August 2012 - 04:28 PM
I see, it makes sense now! Good eye!

When I'm half asleep, I make horrible typos. When I'm awake and in programming mode, I spot things like this. It's a fair trade off :P/>/>
ciba43 #10
Posted 12 August 2012 - 04:37 PM
When I'm half asleep, I make horrible typos. When I'm awake and in programming mode, I spot things like this. It's a fair trade off :P/>/>
For me it reverse. I am in my programming mode at night.
Pharap #11
Posted 12 August 2012 - 04:47 PM
When I'm half asleep, I make horrible typos. When I'm awake and in programming mode, I spot things like this. It's a fair trade off :P/>/>
For me it reverse. I am in my programming mode at night.

Oh, I'm not tired at night, I'm tired at 6am in the morning when I've been up all night :D/>/>
robertgoogle #12
Posted 12 August 2012 - 07:15 PM
thanks for helping me and doing so fast
cant_delete_account #13
Posted 12 August 2012 - 07:25 PM
Here's the corrected code for spelling and the mon being assigned too late.

Sorry, but my programming brain has just kicked into gear and I've spotted some improvements that could be made:


while true do
local side = "right"
local mon = peripheral,wrap(side, true)
mon.setTextScale(1)
mon.setCursorPos(1,1)
if rs.getInput("left", "true") then
mon.write("power ON")
else
mon.write("OUT OF POWER")
end
sleep(2.0)
mon.clear()
end

Condensed by 4 lines. I probably wouldn't have noticed that were it not for the repost.
peripheral,wrap?
Fixed and condensed even more:

while true do
local mon = peripheral.wrap("right", true)
mon.setTextScale(1)
mon.setCursorPos(1,1)
if rs.getInput("left", "true") then
mon.write("power ON")
else
mon.write("OUT OF POWER")
end
sleep(2.0)
mon.clear()
end