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

Keep calling nil?! [Solved] [New problem(end expected)] :P

Started by nlioc4, 14 August 2012 - 10:29 AM
nlioc4 #1
Posted 14 August 2012 - 12:29 PM
I spent the last hour or so repeatedly trying everything I can think of that could cause this. I'm trying to write a program to simply control 3 outputs from a bundled cable for our tekkit refinery. Currently the program is only have way done, but when I'm trying to test what I've written so far, it keeps calling nil on random lines for no apparant reason, at least to me. Please help me, I'd hate to see over 2 hours of my time be for nothing :P/>/> Maybe I'm just tired so I can't see the problem, it's around 4:30 am here atm. If you don't understand what a certain function is for feel free to ask me to clarify.


local clients = 31
local pass = "4808"
local modemside = "top"
local cableside = "left"
local oil = colors.green
local refinery = colors.blue
local fuel = colors.white
local oilstat = {}
oilstat[1] = on
local fuelstat = {}
fuelstat[1] = on
local refinerystat = {}
refinerystat[1] = on
local cablestat = {}
cablestat[1] = oil
cablestat[2] = refinery
cablestat[3] = fuel
function MainMenu()
while true do
term.clear()
term.setCursorPos(1, 1)
print("Refinery Control")
print("Please choose a system")
print("1. Oil Tank pumps")
print("2. Fuel Tank Pumps")
print("3. Refinery Control")
input = read()
if input == "1" then
  OilChoice()
end
if input == "2" then
  FuelChoice()
end
if input == "3" then
  RefineryChoice()
end
end
function OilChoice()
  while true do
   term.clear()
   term.setCursorPos(1, 1)
   print("Oil Tank Pump Control")
   if oilstat == "on" then
	print("Pumps are currently on, would you like to disable? Y/N?")
	input = read()
	if input == "Y" then
	 print("Disabling Oil Pumps...")
	 table.remove(oilstat)
	 table.insert(oilstat,off)
	 table.insert(cablestat,1,oil)
	 sleep(1)
	 print("Oil Pumps disabled!")
	  sleep(2)
	 end
	else
	 print(Ok)
	 sleep(2)
	 end
   end
   if oilstat[1] == off then
	print("Pumps are currently off, would you like to enable? Y/N?")
	 input = read()
	if input == "Y" then
	 print("Enabling Oil Pumps...")
	 table.remove(oilstat)
	 table.insert(oilstat,on)
	 table.remove(cablestat,1)
	 sleep(1)
	 print("Oil Pumps enabled!")
	 sleep(2)
	end
	else
	 print(Ok)
	 sleep(2)
	end
   end
function FuelChoice()
term.clear()
term.setCursorPos(1, 1)
print("Fuel Pump Control")
if fuelstat[1] == on then
  print("Pumps are currently on, would you like to disable? Y/N?")
  input = read()
  if input == "Y" then
   print("Disabling Fuel Pumps...")
   table.remove(fuelstat)
   table.insert(fuelstat,off)
   table.insert(cablestat, 2, fuel)
   sleep(1)
   print("Fuel Pumps disabled!")
   sleep(2)
  end
  else
   print(Ok)
   sleep(2)
  end
   if fuelstat[1] == off then
	print("Pumps are currently off, would you like to enable? Y/N?")
	 input = read()
	if input == "Y" then
	 print("Enabling Fuel Pumps...")
	 table.remove(fuelstat)
	 table.insert(fuelstat,on)
	 table.remove(cablestat, 2)
	 sleep(1)
	 print("Fuel Pumps enabled!")
	 sleep(2)
	end
	else
	 print(Ok)
	 sleep(2)
	end
   end
function RefineryChoice()
   term.clear()
   term.setCursorPos(1, 1)
   print("Refinery Pump Control")
   if refinerystat[1] == on then
	print("Pumps are currently on, would you like to disable? Y/N?")
	input = read()
	if input == "Y" then
	 print("Disabling Refinery Pumps...")
	 table.remove(refinerystat)
	 table.insert(refinerystat,off)
	 table.insert(cablestat, 3, refinery)
	 sleep(1)
	 print("Refinery Pumps disabled!")
	 sleep(2)
	end
	else
	 print(Ok)
	 sleep(2)
	end
   end
   if refinerystat[1] == off then
	print("Pumps are currently off, would you like to enable? Y/N?")
	 input = read()
	if input == Y then
	 print("Enabling Refinery Pumps...")
	 table.remove(refinerystat)
	 table.insert(refinerystat,on)
	 table.remove(cablestat, 3)
	 sleep(1)
	 print("Refinery Pumps enabled!")
	 sleep(2)
	end
	else
	 print(Ok)
	 sleep(2)
	end
   end
while true do
  rednet.open(modemside)
  term.clear()
  term.setCursorPos(1, 1)
  print("Refinery Control, please enter the password:")
  input = read("*")
  if input == pass then
   print("Password Correct!")
   sleep(1)
   MainMenu()
  if input == "Xieylu" then
   break
   end
  else
   print("Password Incorrect!")
   sleep(2)
  end
end
   
sjele #2
Posted 14 August 2012 - 12:58 PM
Idk if this is casuing it but it is worth a try :P/>/>

print(ok)
--Should be
print("ok")
Right now you are trying to print a var that is nothing.
BigSHinyToys #3
Posted 14 August 2012 - 01:26 PM
I have to say this code is a bit messy but i think i have ironed out enough wrinkles This should work I have not added bundle controls as this code is not finished and it is better for you to do that and gain some more code experience. If you have a problem adding bundle cable control then post here and i will add it for you
http://pastebin.com/Mf4raNcN

Idk if this is casuing it but it is worth a try :P/>/>

print(ok)
--Should be
print("ok")
Right now you are trying to print a var that is nothing.
that line never get called so it never errors there
Lyqyd #4
Posted 14 August 2012 - 03:06 PM
Your functions are being declared after their first use. Move the function declarations to the top of the file.
nlioc4 #5
Posted 14 August 2012 - 11:47 PM
Thanks guys, that solved it. Yeah sorry its messy, I wrote that very late at night so I'm not sure how clearly I was thinking. I've added a server piece to it, and i'm finishing up a client so I can also control my refinery from afar. Now I just keep getting "end" expected to close while, even though I have closed it properly, at least in my opinion. I'll finish up my attempt, then post both the server and client if they're still not working. I really appreciate the help as I am very new to LUA and coding in general.
nlioc4 #6
Posted 14 August 2012 - 11:51 PM
I think I've isolated the problem. When I add this section of code I get the error bios:206: [string 'refinery']:184: 'end' expected (to close 'while' at line 176) Line 176 is the first line in this segment. Line 184 is the line with the first elseif.
while true do
	local id, operation
	repeat
	id, operation = rednet.receive()
	until id == clients
	if operation == "oilstat" then
		rednet.send(clients, oilstat)
	end
	elseif operation == "fuelstat" then
		rednet.send(clients, fuelstat)
	end
	elseif operation == "refinerystat" then
		rednet.send(clients, refinerystat)
	end
	elseif operation == "oiloff" then
		table.remove(oilstat)
		table.insert(oilstat,off)
		table.insert(cablestat,1,oil)
		sleep(1)
		rednet.send(clients, "complete")
	end
	elseif operation == "oilon" then
		table.remove(oilstat)
		table.insert(oilstat,on)
		table.remove(cablestat,1)
		sleep(1)
		rednet.send(clients, "complete")
	end
	elseif operation == "fueloff" then
		table.remove(fuelstat)
		table.insert(fuelstat, off)
		table.insert(cablestat,2,fuel)
		sleep(1)
		rednet.send(clients, "complete")
	end
	elseif operation == "fuelon" then
		table.remove(fuelstat)
		table.insert(fuelstat, on)
		table.remove(cablestat,2)
		sleep(1)
		rednet.send(clients, "complete")
	end
	elseif operation == "refineryoff" then
		table.remove(refinerystat)
		table.insert(refinerystat, off)
		table.insert(cablestat,3,refinery)
		sleep(1)
		rednet.send(clients, "complete")
	end
	elseif operation == "refineryon" then
		table.remove(refinerystat)
		table.insert(refinerystat, on)
		table.remove(cablestat,3)
		sleep(1)
		rednet.send(clients, "complete")
	end
end
Correct me if I'm wrong but that means it thinks I'm missing an "end" right? But I have one right at the end there? I'm confused :P/>/>
BTW this code is from the server section, and is where the server receives rednet messages which tell it what to turn on or off.

Help greatly appreciated :D/>/>
BigSHinyToys #7
Posted 15 August 2012 - 08:18 AM
I am currently working on your code. the main probblem is your use of IF's this is an example IF note the end's

if user == "hello" then
    print("hello hello")
elseif user == "fred" then
    print("hello fred")
elseif user == "random" then
    print("hello random")
end
there is one END at the bottom ELSEIF's don't require an end as they all share the one provided by IF at the top

I will correct these error will take some time.
BigSHinyToys #8
Posted 15 August 2012 - 08:51 AM
I have fixed all the syntax errors I would recommend you go through each program line by line and look at how I yse If's ELSEIF's.
I have not fixed the logic errors logic errors are errors where the way the code is written will run but wont do what you inted it to do. currnetly your program docent communicate with the server and vice versa. The server doesn't get to a position where it will interpret the messages. you need to rethink how the server and client interact.


Server http://pastebin.com/J8ifcJrS
Client http://pastebin.com/0JJgcqgR
nlioc4 #9
Posted 15 August 2012 - 09:08 AM
Yeah I think I rushed into just writing code to quickly. I've done some tutorials now and think I have a better understanding of how this all works. Thanks for the help :P/>/>