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

[Lua][Help][Comp.]

Started by Vortex, 26 March 2013 - 09:27 PM
Vortex #1
Posted 26 March 2013 - 10:27 PM
Hello guys ! :)/>
I'm a beginner Lua coder, I've programmed little programs such as a login program (Available here: http://www.computercraft.info/forums2/index.php?/topic/11526-compcraftos-v15security-login/page__pid__102244#entry102244) and I'm trying harder stuff to train myself at Lua. I'm hosting a small whitelisted server with friends with CC, and I tried to code something to store RedNet sent messages on a floppy, unfortunatly, I managed to store one-worded messages on a valuable stored on a floppy, but then the program was erased once a new message was sent, I need help - according the fact it's possible - to store multiple words and multiple messages from RedNet on a floppy disk, if possible. Can any of you help me ? ;)/>
jag #2
Posted 26 March 2013 - 10:38 PM
Most people thinks it's bad to give out "free" code like this, but I like to do that to nice people, at least when it's simple.

Okey, let's begin.
If you don't understand something just ask!

Spoiler

local diskside = "right, left, up, whatever you got"
local rdntside = "same as diskside, just where your modem is"
local path = ""
local fileName = "tmp"

local function checkDisk()
	while not disk.hasData(diskside) then
		sleep(.5)
	end
	path = disk.getMountPath(diskside) .. "/"
end

local function save(x)
	checkDisk()
	local file = fs.open(path..fileName, "a")
	file.writeLine(x)
	file.close()
end

local function receive()
	if not rednet.isOpen(rdntside) then
		rednet.open(rdntside)
	end
	local id,msg = rednet.receive()
	local y = "[" .. id .. "] " .. msg
	save(y)
end

while true do
	receive()
end

And so there you have it. I hope you can see what the code does!
I've tested it and it works. So you can just plop it onto a computer, make some minor modifications, and then just run it.
SadKingBilly #3
Posted 27 March 2013 - 03:12 AM
You didn't actually show us the code you have that isn't working, so it's hard to guess what the problem is. But I assume you're opening a file stored on the floppy disk in 'write' mode instead of in 'append' mode. So, if there's a line in your code that looks like "file = fs.open(path, "w")", then you need to replace it with "file = fs.open(path, "a")".
jag #4
Posted 27 March 2013 - 04:19 AM
Yea as TheCoryKid says, the mode you open the file in is very important. But due to that I do not believe that is the main problem. Could you give us your current code?
Vortex #5
Posted 27 March 2013 - 08:59 PM
Actually I've deleted it :/
jag #6
Posted 27 March 2013 - 10:41 PM
Oh well, then we cannot help you much longer. You'll have to give us some proper questions to get a proper answer.
Vortex #7
Posted 30 March 2013 - 06:48 AM
I just seek some program that registers messages from Rednet on a floppy so I can manage eventual abuses.
jag #8
Posted 30 March 2013 - 06:50 AM
Well then just use my code.