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

Help with a program for a mob farm

Started by bultador, 26 February 2015 - 10:11 PM
bultador #1
Posted 26 February 2015 - 11:11 PM
i'm trying to make a little program where i can change my mob farm to automated killing with turtles, or manual killing (and the turtles move away). i want to control everything from a central computer next to the turtles.

This is my turtle program:


rednet.open("right")
local place = 0
local function moveTo()
  turtle.down()
  turtle.forward()
end
local function moveFrom()
  turtle.back()
  turtle.up()
end
local function check()
  while true do
	event, text = os.pullEvent()
	if event == "rednet_message" then
	  if text == "KILL" then
		if place == 0 then
		  moveTo()
		  place = 1
		end
	  else if text == "BACK OFF" then
		if place == 1 then
		  moveFrom()
		  place = 0
		end
	  end
	end
  end  
end
end --this one is somehow necessary else it will say it expected an end...
local function attack()
  while true do
  turtle.attack()
  end
end
parrallel.waitforAny(attack, check)

and this is my computer code:


rednet.open("back")
while true do
  lever = redstone.getInput("right")
  if lever == true then
	for i = 1, 5 do
	  rednet.broadcast("KILL")
	  sleep(0.1)
	end
	sleep(10)
  else
	for i = 1, 5 do
	  rednet.broadcast("BACK OFF")
	  sleep(0.1)
	end
	sleep(10)
  end
end
(ignore the wierd "end" placement, the site doesn't like my placement somehow.)
The one problem i have is that the turtle program keeps giving the error (killer:39: attempt to index ? (a nil value)).
What am I doing wrong?
HPWebcamAble #2
Posted 27 February 2015 - 12:44 AM
Ok, first, your turtle program is 36 lines. So line 39 can't be erroring.
That might be just because your code is different lengths on the forum and in-game, so not a big deal

It think your problem is this line:

event, text = os.pullEvent()
The event 'modem_message' returns the following arguments:
senderID,message,protocol

Since you only capture the first two, you get the event (which is actually the first) and the senderID.
All you need to do is this:

event, sID, text = os.pullEvent()
That should fix that problem

The extra end is because of this:

else if text == "BACK OFF" then
It should be 'elseif', one word, not 'else if'

(The else needs an end, AND the if wants an end so that's why it needs an extra)

Good Luck with your mob spawner :)/>
bultador #3
Posted 27 February 2015 - 01:05 PM
Ok, first, your turtle program is 36 lines. So line 39 can't be erroring.
That might be just because your code is different lengths on the forum and in-game, so not a big deal

It think your problem is this line:

event, text = os.pullEvent()
The event 'modem_message' returns the following arguments:
senderID,message,protocol

Since you only capture the first two, you get the event (which is actually the first) and the senderID.
All you need to do is this:

event, sID, text = os.pullEvent()
That should fix that problem

The extra end is because of this:

else if text == "BACK OFF" then
It should be 'elseif', one word, not 'else if'

(The else needs an end, AND the if wants an end so that's why it needs an extra)

Good Luck with your mob spawner :)/>

Thanks for the help with the extra "end". The problem isn't the os.pullEvent() though, I had the full line in before and it still didn't work…
I changed it now to what you said and it is still giving the same error. It doesn't like the parrallel.waitforAny line. Any other ideas?
GopherAtl #4
Posted 27 February 2015 - 01:50 PM
there's only 1 r in parallel.
bultador #5
Posted 27 February 2015 - 05:41 PM
there's only 1 r in parallel.

yeah i found it! it now works :)/> My native language isn't english and on this website: http://stackoverflow.com/questions/15673312/lua-computercraft-listen-but-also-function a guy talks about parrallel with 2 r's so that's where it went wrong.