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

[LUA] Whats wrong with this code?

Started by Kexus, 19 June 2012 - 04:54 PM
Kexus #1
Posted 19 June 2012 - 06:54 PM
Im pretty new to ComputerCraft and Lua, although I do have some experience using other languages, so I'm hoping someone could help me figure out why this program isn't working the way it should. I made a simple program for my turtle, where it listens for a rednet message, and preforms a certain action, depending on what it receives. If it receives a message that isn't one of the things it reacts to, it broadcasts "error", and the program ends. When I run the program, it broadcasts "error", no matter what message it receives.

print("Minion Mode Engaged")
rednet.open("right")
while true do
id, message = rednet.receive()
print("Message receive")
if message == "go" then
	turtle.forward()
elseif message == "up" then
	turtle.up()
elseif message == "down" then
	turtle.down()
elseif message == "left" then
turtle.turnleft()
elseif message == "right" then
turtle.turnright()
elseif message == "detect" then
rednet.broadcast(turtle.compare)
elseif message == "done" then
break
else rednet.broadcast("Error "..message )
break
end
end
I'm sure I'm just making some simple mistake, but I don't know what it is.
MysticT #2
Posted 19 June 2012 - 07:03 PM
That code is fine, the problem must be in the sender, or maybe there's other computer broadcasting, so it gets the wrong message.

Also, you should change this line:

rednet.broadcast(turtle.compare)
to this:

rednet.broadcast(tostring(turtle.compare())) -- added () and tostring()
Kexus #3
Posted 19 June 2012 - 07:22 PM
You're right. The code was fine. I had a temporary moment of stupidity. The 2 commands I tried sending it were "forward" and "back", which, if you examine the code, you'll see are not in it. Although I did use turtle.turnleft and .turnright instead of turnLeft, and turnRight. You were also right about adding tostring. Without it it gives an error message saying "String expected". I never would have been able to figure out to fix that. Thanks :(/>/>