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

odd copying error any help?

Started by JohnTheDemon, 15 August 2012 - 06:37 PM
JohnTheDemon #1
Posted 15 August 2012 - 08:37 PM
so i have been writing a code that first searches the computer's side for any peripheral (this is for smp so no one will have any other type of peripheral other than a disk drive) and then sets the variable g to true and then an if statement to check weather both g and os.exists("startup") == true so then i have it to delete the startup on the computer and then copy the startup from the disk to the computer i'll show you the bit of the code that doesnt work but basically it doesnt copy or do anything that the code written would sudgest it would do and i have tried reloading the saved file and all of the things that would correct it so here it is thanks!:



a = peripheral.isPresent("left")
b = peripheral.isPresent("right")
c = peripheral.isPresent("top")
d = peripheral.isPresent("bottom")
e = peripheral.isPresent("front")
f = peripheral.isPresent("back")

if a == true then
g = true
else
if b == true then
g = true
else
if c == true then
g = true
else
if d == true then
g = true
else
if e == true then
g = true
else
if a == true then
g = true
else
g = false
end
end
end
end
end
end
if g == true and fs.exists("startup") then
fs.delete("startup")
sleep(0.1)
fs.copy("disk/startup","")
else
fs.copy("disk/startup","")
end

if g == true and fs.exists("disk/startup") then
fs.delete("disk/startup")
sleep(0.1)
fs.copy("startup","disk/startup")
else
fs.copy("startup","disk/startup")
end
end
hope you can help my error! thanks
JohnTheDemon #2
Posted 15 August 2012 - 08:41 PM
also before i forget to say this it also gives no error message or anything at all thanks!
Cranium #3
Posted 15 August 2012 - 08:44 PM
You want elseif in stead of else if. Should be one word.
JohnTheDemon #4
Posted 15 August 2012 - 08:51 PM
thanks ill try it i'll be right back

oh and also how do i do the in the code in the box it would be much easier to read
JohnTheDemon #5
Posted 15 August 2012 - 08:56 PM
ah… it does make the code much better code wise it doesn't fully fix my problem… i still have the issue where it doesn't want to copy it to the computer after deleting it any help would be appreciated but i could probably sort it. however does it matter that the script is within a while statement?
OmegaVest #6
Posted 15 August 2012 - 08:57 PM
Craniumkid22 is right, but this can also be done much better.


local side = ""

for k, v in pairs(rs.getSides) do
   if peripheral.isPresent(v) then
	  side = v
	  break
   end
end

if side and fs.exists("disk/startup") then
   if fs.isDir("disk") then
      fs.delete("disk/startup")
      sleep(0.1)
      if not fs.exists("disk/starup") then
         fs.copy("startup","disk/startup")
      else
         print("Bork'd")
      end
   else
      fs.copy("startup","disk/startup")
   end
else
   print("'disk' is not the name of the directory")
end

Also, for code tags, put [ and ] around the word code at the top, and then around /code at the bottom of the code.

EDIT: Added in a small check to see if it worked or not.
JohnTheDemon #7
Posted 15 August 2012 - 10:06 PM
how does one use code tags? i am relatively new to complex coding in lua i am still a low level coder you could say and is this why the code comes up with an error telling me it wants a table but it got a function…

thanks
Cranium #8
Posted 15 August 2012 - 10:17 PM
just put the word "code" in square brackets [ ] at the start, and /code in square brackets at the end. Or you can use the button that looks like this: <> to start your code block.
Lettuce #9
Posted 16 August 2012 - 01:11 AM
just put the word "code" in square brackets [ ] at the start, and /code in square brackets at the end. Or you can use the button that looks like this: <> to start your code block.
Also, as a courtesy, if your code is long (yours isn't this time) but say, 100+ lines, spoiler tag it. so it looks like this (ignore the asterisks):
[*spoiler]
[*code]
– code goes here.
[*/code]
[*/spoiler]

Spoiler

Will look like this.
JohnTheDemon #10
Posted 16 August 2012 - 11:51 AM
thankyou guys! that helped me look instantly cooler
JohnTheDemon #11
Posted 16 August 2012 - 12:01 PM

local side = ""

for k, v in pairs(rs.getSides) do
   if peripheral.isPresent(v) then
	  side = v
	  break
   end
end

if side and fs.exists("disk/startup") then
   if fs.isDir("disk") then
	  fs.delete("disk/startup")
	  sleep(0.1)
	  if not fs.exists("disk/starup") then
		 fs.copy("startup","disk/startup")
	  else
		 print("Bork'd")
	  end
   else
	  fs.copy("startup","disk/startup")
   end
else
   print("'disk' is not the name of the directory")
end

i have tried this however i get no luck and infact it just gives me an error message this time: bad argument: table expected, got function and i have no idea how to change it from a function to a table because to be frank i thought a function was function
functionname()
Cloudy #12
Posted 16 August 2012 - 02:35 PM
for k, v in pairs(rs.getSides) do
Should be:
for k, v in pairs(rs.getSides()) do

Also, your statement that as its SMP the only peripheral would be a disk drive is incorrect - it could also be a modem or a monitor. Your check should be:

if peripheral.getType(v) == "disk" then
OmegaVest #13
Posted 16 August 2012 - 03:13 PM
:(/>/>

Always one error. Never more, never less. (Most times, anyway)
JohnTheDemon #14
Posted 16 August 2012 - 04:50 PM
thanks! guys this really helped cleaning up my code however it still somehow doesn't copy the startup file from the disk to the computer =(

thanks
JohnTheDemon #15
Posted 16 August 2012 - 04:54 PM
i dont need it to work actually this has really helped but i am going to stop making the code and start a new one: a player sensor using the ccsensors proximity sensor majigy however i am horrible at coding the ccsensors api and whenever i make anything with it it seems just to bug out and not work any help i'll start a new topic soon as soon as i realise how to close this one