What's wrong here? (txt file keeps getting appended)

I’m trying to record the time and state of a panel being pressed. I have variables for the buttons (pressed or not). I’m trying to record the time as well. I should be able to put the variable for the time it was pressed into the index of the table. But when I do, it seems to work but it keeps adding to the file like it’s being appended. Should file.Write literally write over it…something in the ingame memory is saving it somehow… is there something wrong below?




net.Receive("Var1Set", function ()

elapsed = CurTime() - start_time
function SecondsSinceStart()
return CurTime() 

 end

Var1 = "Var 1 is SET"
mytable[elapsed] = "The Time Var1 was set"


end
net.Receive("Var2Set", function ()

elapsed = CurTime() - start_time
function SecondsSinceStart()
return CurTime() 

end

Var2 = "Var 2 is SET"
mytable[elapsed] = "The Time Var2 was set"


end

util.AddNetworkString("writelog")
net.Receive("writelog", function ()
mytable = { Var1 ,
		Var2
				}


mytableprint =   util.TableToJSON(mytable, true)


file.Write( "Logfile.txt" , mytableprint)

end 

In terms of your file.Write everything seems fine. It should overwrite everything in the file. Can you give us an example logfile of what you expect, and what the logfile actually contains?

It’s hard to say…right now i’ve removed all this code and just made a variable for every single button pressed for the time as well…then just add all variables at the end into a table…

Before I was getting the multiple tables it seemed, even though I was along calling one when I would write to file.

Well if you don’t know what you expect in the output, how can you be sure that the output is wrong? Right now it sounds like you just “have a feeling” that it is appending and not writing. We can’t help you with a problem that you aren’t sure exists.

In the writelog net message callback you are redeclaring your mytable global, causing any values you had originally assigned to be lost, i.e. your mytable[elapsed] = “Stuff” lines.



...
Var1 = "Var 1 is SET"
mytable[elapsed] = "The Time Var1 was set"
...
Var2 = "Var 2 is SET"
mytable[elapsed] = "The Time Var2 was set"
...
mytable = { Var1 , Var2    } -- this just destroyed anything you previously had in the table, i.e. your timestamps

No offense, but your code is confusing and looks hacky. Whatever you’re trying to accomplish could be done in a much simpler/cleaner way, but without the full code, can’t really help much.

Yeah I’ve been building it piece by piece. Each time I learn something I move on…should probably pick up my toys before I do so…

I don’t have the best understanding of lua, my knowledge is very piecemeal and fragmented…