what does 'gmatch' (string expected, got nil) mean?

Hello, since the last update, my logging system broke. I hate messing with string’s, so I’m asking you what I can do to fix this little line.

[lua]
local logrecs = string.Explode("
", file.Read(“logging/log.txt”))
[/lua]
This is the only code that breaks my logging system.

Exploding by "
" never worked, use this instead:

[lua]
for logline in string.gmatch(file.Read(“logging/log.txt”), “%C+”) do
print(logline)
–stuff
end
[/lua]

I’m using it on a DListView, and it worked like some weeks ago.

Have you atleast tried using that one?

I’m going to test it now.

[editline]26th February 2011[/editline]

Yeah, that worked. Thanks.

Remember, that code will fuck up if you tabs and other special charachter in your text file, I think people like raBBish can give you a better solution for breaking on newlines.

Indeed.

[lua]for line in (file.Read(“somefile”) or “”):gmatch("[^
\r]+") do
– stuff
end[/lua]

raBBish, providing better patterns since 2007.
I just assumed that escape charachters didn’t work in patterns since they don’t work in string.Explode.

Okay, but I can’t seem to get the user to read the file trough a usermessage. Maybe it’s caused by the file being in addons?

[editline]26th February 2011[/editline]

It does write to my server when someone dies, but doesn’t read it and put it on the DListView.

They work fine in string.Explode.

Anyone has any idea what can cause it to not be able to read the file?

  1. A null byte

There is no other reason for being unable to read a text file in GMod.

[editline]26th February 2011[/editline]

Post your code to read the file and the contents of the file.

Yeah, will do.

[lua]
– sv_logging.lua

function playerDies( victim, inflictor, attacker )
if inflictor:IsPlayer() then
inflictor = inflictor:GetActiveWeapon()
end
if attacker == victim then
filex.Append(“logging/log.txt”, tostring(os.date("%d/%m/%y"))…" - “…tostring(os.date(”%H:%M:%S"))…" - “…attacker:Name()…” suicided!"… "
“)
else
filex.Append(“logging/log.txt”, tostring(os.date(”%d/%m/%y"))…" - “…tostring(os.date(”%H:%M:%S"))…" - “…attacker:Name()…” killed "…victim:Name()… " with "…inflictor:GetClass()… "
")
end
end
hook.Add( “PlayerDeath”, “playerDeathTest”, playerDies )
[/lua]

[lua]
– cl_logging.lua

local LoggingListView = vgui.Create(“DListView”)
LoggingListView:SetParent(LoggingMenu)
LoggingListView:SetPos(3, 50)
LoggingListView:SetSize(454, 375)
LoggingListView:SetMultiSelect(false)
LoggingListView:AddColumn(“Entry”)
for logrecs in (file.Read(“logging/log.txt”) or “”):gmatch("[^
\r]+") do
LoggingListView:AddLine( logrecs ) end
LoggingListView.OnRowSelected = function(self, row)
local confirmation = DermaMenu()
confirmation:AddOption(“Copy to Clipboard”, function() SetClipboardText(self:GetLine(row):GetValue(1)) end)
confirmation:Open()
end
[/lua]

Contents of logging/log.txt?

[editline].[/editline]

Also, are you networking the contents to the client? At the moment you’re reading from a file when I presume is empty, since the file is being read from the client not from the server.

I told him he needed to network it to the clients, you can’t read files directly from the server.

Drew, I though I understood you.

[editline]27th February 2011[/editline]



-- Content of log.txt
26/02/11 - 17:12:24   -   Persious suicided!
26/02/11 - 17:15:15   -     suicided!
26/02/11 - 17:15:27   -   Persious suicided!
26/02/11 - 17:25:36   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:25:47   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:02   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:12   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:20   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:29   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:41   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:26:52   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:27:02   -   dP | Neon Mkay <3 suicided!
26/02/11 - 17:38:38   -   Persious suicided!
26/02/11 - 17:43:27   -   Persious suicided!
26/02/11 - 17:47:52   -   Persious killed dP | Neon Mkay <3  with  weapon_pistol
26/02/11 - 17:48:12   -   Persious killed dP | Neon Mkay <3  with  crossbow_bolt
26/02/11 - 17:48:27   -   Persious killed dP | Neon Mkay <3  with  crossbow_bolt
26/02/11 - 17:48:36   -   Persious killed dP | Neon Mkay <3  with  crossbow_bolt
26/02/11 - 17:48:50   -   Persious killed dP | Neon Mkay <3  with  crossbow_bolt
26/02/11 - 17:49:05   -   Persious killed dP | Neon Mkay <3  with  crossbow_bolt
26/02/11 - 17:49:11   -   dP | Neon Mkay <3 killed Persious  with  weapon_ar2
26/02/11 - 17:49:26   -   Persious killed dP | Neon Mkay <3  with  weapon_smg1
26/02/11 - 17:49:29   -   Persious killed dP | Neon Mkay <3  with  weapon_357
26/02/11 - 17:49:35   -   dP | Neon Mkay <3 killed Persious  with  weapon_smg1
26/02/11 - 17:50:03   -   dP | Neon Mkay <3 killed Persious  with  weapon_smg1
26/02/11 - 17:50:17   -   dP | Neon Mkay <3 killed Persious  with  weapon_smg1
26/02/11 - 17:51:05   -   dP | Neon Mkay <3 killed Persious  with  weapon_ar2
26/02/11 - 17:51:52   -   dP | Neon Mkay <3 killed Persious  with  weapon_357
26/02/11 - 17:52:33   -   Persious killed dP | Neon Mkay <3  with  prop_physics
26/02/11 - 17:52:52   -   dP | Neon Mkay <3 killed Persious  with  weapon_357
26/02/11 - 17:52:52   -   Persious killed dP | Neon Mkay <3  with  prop_physics
26/02/11 - 17:52:59   -   Persious killed dP | Neon Mkay <3  with  prop_physics
26/02/11 - 17:53:07   -   Persious killed dP | Neon Mkay <3  with  prop_physics
26/02/11 - 17:53:28   -   dP | Neon Mkay <3 killed Persious  with  rpg_missile
26/02/11 - 17:53:34   -   dP | Neon Mkay <3 killed Persious  with  rpg_missile
26/02/11 - 17:53:40   -   dP | Neon Mkay <3 killed Persious  with  rpg_missile


You really need to network that to the clients. I’ll probably get slated for this, but I’d use the datastream, since splitting a string that size into 252 bytes would be a tedious task.

Then I’ll need to learn how to use datastream, but thanks!