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

Rednet Connector Help

Started by x_S_5_S_x, 27 December 2013 - 10:38 PM
x_S_5_S_x #1
Posted 27 December 2013 - 11:38 PM
Please Help Me Figure Out What Is Wrong. There are no output errors. The program will not connect to the modem on the back of the computer.

http://pastebin.com/1ZLahKSM
John2143658709 #2
Posted 28 December 2013 - 12:42 AM
Right at line one :)/>
I assume that you want a table there, not just random statements

sides={"top";"right";"left";"back"}

sides={"top","right","left","back"}


Make sure you use commas to seperate the objects in the table
Also, there are some smaller things you can change, like use an iterator to loop through the table because you dont need the index, and also make the user inputs all lowercase for consistancy
This also doesnt work, it only checks one side then instantly fails


rside=read()
for i=1,#sides do
    if rside==sides[i] then
        print "Connecting Modem..."
        os.sleep(1)
        rednet.open(rside)
        print "Modem Connected!"
    else
        print "Couldn't Find Modem. Try Again."
        os.sleep(1)
        os.reboot()
    end
end
local rside=read()
local success = false
for _,v in sides do
    if rside==v then
        --success code here
        success= true
    end
    if !success then
        --failure code here
    end
Edited by
Lyqyd #3
Posted 28 December 2013 - 01:27 AM
Semicolons are perfectly fine to separate table elements. His table is fine. It's also unnecessary, since redstone.getSides() returns a table of all six sides, not just those four. The suggested code above, on the other hand, isn't valid Lua.

I'd suggest replacing the modem finding loop with something like this. It first checks if the user-provided modem side will work, and if it won't, tries to use any available modem. If no modem is found, it prints a message for the user and exits.


local modemSide = false

if peripheral.getType(rside) == "modem" then
	modemSide = rside
else
	for _, side in ipairs(rs.getSides()) do
		if peripheral.getType(side) == "modem" then
			print("Couldn't find modem on "..rside..", falling back to modem on "..side)
			modemSide = side
			break
		end
	end
end

if modemSide then
	rednet.open(modemSide)
else
	print("Could not find modem!")
	return
end

Also, you'll need to change the if statement later on. if blah == "string" or "otherString" then doesn't work, you need to do individual comparisons for each possible string, if blah == "string" or blah == "otherString" then.
x_S_5_S_x #4
Posted 28 December 2013 - 09:58 AM
Thank you to both of you. :D/> It works now!