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

Small Problems with tables

Started by AlkamlBan, 08 July 2014 - 02:25 PM
AlkamlBan #1
Posted 08 July 2014 - 04:25 PM
Hello guys and I came here to obviously ask a question about tables. Now I have been developing a small program for virtual currency where basically there is the computer from which you pay and the server of the bank. I have coded both but seem to get something wrong with the table (I suck at using tables). So here is the code for the server:

--Setting up Host
rednet.open("right")
rednet.host("bankServer", "MineBank")
customers = {{0, 1000}, {1, 550}}
while true do
    --Gets Intel
    sender, ID = rednet.receive()
    sender, amount = rednet.receive()
    for i = 0, #customers do
	    if customers[i][0] == ID then
		    if customers[i][1] >= amount then   
			    rednet.send(sender, "Done")
			    customer[i][1] = customer[i][1] - amount
		    else
			    rednet.send(sender, "Not Done")
		    end
	    end
    end  
end  

When I run this it gives me the following error after a small delay:
startup:10: attempt to index ? (a nil value)

Here is the code for the computer in case it helps:

amount = 100
--Setting up
rednet.open("right")
--Finding bank and disk ID
bankServer = rednet.lookup("bankServer")
payerID = disk.getID("bottom")
--Sending Request
rednet.send(bankServer, payerID)
rednet.send(bankServer, amount)
bank, message = rednet.receive()
if message == "Done" then
    print("Transaction Complete")
else
    print("Transaction Failed")
end
Emma #2
Posted 08 July 2014 - 05:15 PM
The reason ur getting attempt to index nil is because (i am assuming) u are used to other languages where the indexes start at 0, then 1,2,3,etc.. in lua, it starts at 1.
Fix:

--Setting up Host
rednet.open("right")
rednet.host("bankServer", "MineBank")
customers = {{0, 1000}, {1, 550}}
while true do
	--Gets Intel
	sender, ID = rednet.receive()
	sender, amount = rednet.receive()
	for i = 0, #customers do
			if customers[i][1] == ID then
					if customers[i][2] >= amount then  
							rednet.send(sender, "Done")
							customer[i][2] = customer[i][2] - amount
					else
							rednet.send(sender, "Not Done")
					end
			end
	end  
end
OTHER:

rednet.host("bankServer", "MineBank")
customers = {{[0]=0, [1]=1000}, {[0]=1, [1]=550}} --Before it was [1]=0,[2]=1000
while true do
If you are trying to get the index because its value is set to 0 then u must do [0]=0 not just 0 as defined here:
Edited on 09 July 2014 - 10:47 PM
AlkamlBan #3
Posted 08 July 2014 - 06:33 PM
The reason ur getting attempt to index nil is because (i am assuming) u are used to other languages where the indexes start at 0, then 1,2,3,etc.. in lua, it starts at 1.
Fix:

--Setting up Host
rednet.open("right")
rednet.host("bankServer", "MineBank")
customers = {{0, 1000}, {1, 550}}
while true do
	--Gets Intel
	sender, ID = rednet.receive()
	sender, amount = rednet.receive()
	for i = 0, #customers do
			if customers[i][0] == ID then
					if customers[i][1] >= amount then  
							rednet.send(sender, "Done")
							customer[i][1] = customer[i][1] - amount
					else
							rednet.send(sender, "Not Done")
					end
			end
	end  
end
OTHER:

rednet.host("bankServer", "MineBank")
customers = {{[0]=0, [1]=1000}, {[0]=1, [1]=550}} --Before it was [1]=0,[2]=1000
while true do
If you are trying to get the index because its value is set to 0 then u must do [0]=0 not just 0 as defined here:

THnx that part is fixed but I still get another error :D/> in the server at line 13 saying the exact same error (with the difference of it being in the line 13 instead of 10 ofcourse)
Bomb Bloke #4
Posted 09 July 2014 - 12:09 AM
customer ~= customers
AlkamlBan #5
Posted 09 July 2014 - 07:44 PM
customer ~= customers
I know I fixed that when I saw it again on the Computer but I still get can't get it to work
Edited on 09 July 2014 - 05:44 PM
Lyqyd #6
Posted 09 July 2014 - 08:31 PM
So post the updated full code and the new full error message.