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

Error: Having trouble using Command Blocks

Started by TheGeek, 01 February 2013 - 03:19 PM
TheGeek #1
Posted 01 February 2013 - 04:19 PM
I'm trying to use CC to control a Command Block and send various commands in a large project I'm working on. But even though I enabled them in the server.properties and the ComputerCraft.cfg files, I get a "attempt to index ? a nil value"

Here's the offending code:



local modemside = "back" --side with the modem on it
local commandside = "right" --side with command block
local noteside = "bottom" --side with iron note block

mon = peripheral.wrap("top") --aquires monitor object
mX, mY = mon.getSize() --gets size of monitor
note = peripheral.wrap(noteside) --get note block for beeps
cmd = peripheral.wrap(commandside)

--start test code
mon.clear()
mon.setCursorPos(1,1)
mon.write("Lets test output to the command block!")
cmd.runCommand("/say this is a test of the command block interface") --this gives me an error
cmd.setCommand("/say this is a test of the command block interface") --ditto

local pwrap = fs.open("pwrap.txt", "w")
for k, v in pairs(cmd) do pwrap.writeLine(tostring(k)) end
pwrap.close() --pwrap.txt is empty!

Its like the .runCommand and .setCommand is never loaded from the peripheral.

What should I try next?
TheArchitect #2
Posted 01 February 2013 - 05:30 PM
I didn't know the vanilla command block behaved like a peripheral. Are you sure you don't need something else?
remiX #3
Posted 01 February 2013 - 05:37 PM
use peripheral.getMethods(cmd) to get the methods of it and then write it to a file
theoriginalbit #4
Posted 01 February 2013 - 05:50 PM
@TheArchitect: yes they can, but they need to be enabled in the CC config to be able to

@TheGeek: Are you playing SMP? If so you need to enable the command block in server.properties
LBPHacker #5
Posted 01 February 2013 - 06:35 PM
Keep in mind that command blocks are a bit buggy at the moment…
Take a look at this topic

Command blocks have three methods:
cmd.setCommand(command)
cmd.runCommand()
cmd.getCommand()

I think they're self explaining.
TheArchitect #6
Posted 01 February 2013 - 06:59 PM
@TheArchitect: yes they can, but they need to be enabled in the CC config to be able to
I haven't seen this mentioned in the wiki. Could you tell me where I can read more on this?
theoriginalbit #7
Posted 01 February 2013 - 07:03 PM
@TheArchitect: yes they can, but they need to be enabled in the CC config to be able to
I haven't seen this mentioned in the wiki. Could you tell me where I can read more on this?
Ummmm idk if there is any documentation on it. I discovered it by accident. If you open ComputerCraft.cfg there is an "enableCommandBlock" or something like that, if you change the false to true then it can be interacted with like a peripheral with the functions LBPHacker stated.
And I know in SMP you also need to enable command blocks in the server.properties file
Luanub #8
Posted 01 February 2013 - 07:15 PM
I don't think you need the / in the function call

cmd.setCommand("/say this is a test of the command block interface") 
--should probably be
cmd.setCommand("say this is a test of the command block interface") 
remiX #9
Posted 01 February 2013 - 09:17 PM
You can have the / or not, makes no difference
TheGeek #10
Posted 02 February 2013 - 05:44 AM
But even though I enabled them in the server.properties and the ComputerCraft.cfg files, I get a "attempt to index ? a nil value"
Yes I enabled them in both the server.properties and the ComputerCraft.cfg files.
I even attempted to get the methods of the peripherial and the file it generated was blank.

Its like the API for the command block isn't being loaded.
augustas656 #11
Posted 27 April 2014 - 07:31 PM
Ermm, my issue was that it was the wrong side, perhaps your command block isn't being read, try replacing the command block maybe? Check your sides?