File Writing

[lua]
function GM:PlayerDeath( victim, weapon, killer )
file.Write(“deaths.txt”, victim:GetPos())
end
[/lua]

Hello!

I am making a logger on where the player’s die in the map in question to see what flaws that map has and where the most deaths are.

After making it I noticed this when I was researching on how to make it
Gmod wiki:



#
# Files are not opened in append mode. Files are emptied before they are written to. 


How would I open it for append mode?

I think you can just not open them in append mode full stop. Though I may be wrong.

You might need to perhaps store all the data by hand each time you write to it. So read the file, store what is in it, add the new data, then write it back including the newest data you wanted to add.

So like?

[lua]

function GM:PlayerDeath( victim, weapon, killer )

local temp = file.Read("…/data/deaths.txt")
file.Write(“deaths.txt”, victim:GetPos() … temp)
end
[/lua]

Well, quickly re-checking the file read/write on the wiki, it looks like it should work. Though I’d suggest some new lines or spaces otherwise all the data is doing to be squashed together with no spacing and it may be difficult to read (unless you don’t intend it to be human readable.) To make a new like use the ’
’ character in your strings.

Ok i tried this:
[lua]

function GM:PlayerDeath( victim, weapon, killer )

file.Write(“deaths.txt”,file.Read("…/data/deaths.txt")… “/n” … victim:GetPos())
end
[/lua]

And got this:



bot
status
hostname: TOD GameMode Server
version : 1.0.0.59/14 3740 secure
udp/ip  :  192.168.1.9:27015
map     : tod_fightyard_b2 at: 0 x, 0 y, 0 z
players : 1 (2 max)

# userid name uniqueid connected ping loss state adr
# 2 "Bot01" BOT active
ulx slay bot
(ADMIN) (Console) slayed Bot01
ERROR: GAMEMODE:'PlayerDeath' Failed: FY/gamemode/init.lua:56: attempt to concat
enate a userdata value


No idea if this would work, but try wrapping victim:GetPos() in tostring() like tostring(victim:GetPos()). Maybe that will work, I think it’s just not being able to write a Vector directly, or whatever is returned by GetPos.

I agree with yakahughes suggestion, and yeah it does return a vector.

tostring should fix that issue.

Not tostring, ToScreen.



Player Bot06 has Won this round!ERROR: GAMEMODE:'PlayerDeath' Failed: FY/gamemod
e/init.lua:71: attempt to call global 'ToScreen' (a nil value)


entity:GetPos():ToScreen()

You obviously didn’t do it right.

And on reply to that



bot
Client "Benjojo12" connected (92.235.150.43:27006).
Player Benjojo12 has Won this round!ERROR: GAMEMODE:'PlayerDeath' Failed: FY/gamemode/init.lua:71: attempt to call global 'ToString' (a nil value)


Here is the source

[lua]

function GM:PlayerDeath( victim, weapon, killer )

local test1337 = ToString(victim:GetPos())
file.Write(“deaths.txt”,file.Read("…/data/deaths.txt")… “/n” … test1337())
end

[/lua]

[editline]06:06PM[/editline]

Well here is your suggestion

[lua]
function GM:PlayerDeath( victim, weapon, killer )

local test1337 = victim:GetPos():ToScreen()
file.Write(“deaths.txt”,file.Read("…/data/deaths.txt")… “/n” … test1337())
end
[/lua]




ERROR: GAMEMODE:'PlayerDeath' Failed: FY/gamemode/init.lua:71: attempt to call method 'ToScreen' (a nil value)


Tostring is lowercase.

[lua]
function GM:PlayerDeath( victim, weapon, killer )

local test1337 = tostring(victim:GetPos())
file.Write(“deaths.txt”,file.Read("…/data/deaths.txt")… “/n” … test1337())
end[/lua]

LOL.

And yes, I said tostring, not ToString, and so did Emz.

I forgot that was for converting vectors to 2d points. Meh. It should have worked.

Appending can be done with filex.Append for future reference.

[lua]
function GM:PlayerDeath( victim, weapon, killer )

file.Write("deaths.txt", file.Read("deaths.txt").."

"…tostring(victim:GetPos()))
end
[/lua]