file.read problem

Hi. Here is my problem, I’m trying to get my DListView to list all of the players trails in their save data and run the command to set their trail but I must be doing it wrong. Thanks for your help. Here is my DListView code
[lua]local DermaListView = vgui.Create(“DListView”, self)
DermaListView:SetPos(15, 160)
DermaListView:SetSize(80, 200)
DermaListView:SetMultiSelect(false)
DermaListView:AddColumn(“Trails”) // Add column

for k,v in pairs(file.Read("player_trails/"..string.Replace(ply:SteamID(),":","_")..".txt")) do      
	DermaListView:AddLine(v) 
end

function DermaListView:OnRowSelected( LineID, Line ) 
	-- Line is a DListView_Line panel object 
	name = Line:GetColumnText( 1 )
	RunConsoleCommand("setTrail",name)
end [/lua]

and here is how the trails are saved if it helps.

[lua]if file.Exists(“player_trails/”…string.Replace(ply:SteamID(),":","")…".txt") then
local In = file.Read(“playert_trails/”…string.Replace(ply:SteamID(),":","
")…".txt")
In = string.Explode("
“,In)
ply.Trails = In;
ply.TrailLoad = true
else
ply.Trails = {}
table.insert(ply.Trails, “”)
local Out = string.Implode(”
“,ply.Trails)
file.Write(“onslaught_trails/”…string.Replace(ply:SteamID(),”:","_")…".txt",Out)
ply.TrailLoad = true
end[/lua]

Once again thanks for any help.

It would help if you told us what the problem is. :smile: Are you getting an error or is it just not working?
Also if I were you I would just use **[Player.SetPData

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Player.SetPData)** and **[Player.GetPData

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Player.GetPData)**.
It’s the easiest way to save player data.

edit : Wait, shouldn’t you be doing the file reading and writing on the server itself and not on your client?

Well the problem is it wrecks my DPanels. Ill have a look at that saving data link and the file reading and writing is server side.

[editline]03:08PM[/editline]

I am now getting this error
(…away\gamemode\osmenu/player_stats.lua:53: attempt to index global ‘ply’ (a nil value))
line 53 is this : for k,v in pairs(file.Read(“onslaught_trails/”…string.Replace(ply:SteamID(),":","_")…".txt")) do

Like it says, ply isn’t defined at the time you are running that code. If you want to save the data of several players you really need to do both the reading and the saving of data on the server and then use usermessages to send the list to the client ( you) so that you can fill your list.

Ok I understand it now. So how do I use usermessages? Would I have to make a function to read the data and then put it here
[lua]for k,v in pairs(function) do
DermaListView:AddLine(v)
end[/lua]

What are you doing?

[lua]
for k,v in pairs(function) do
[/lua]

That was my fail attempt at using usermessages. Basically all i want to do is get the players trail save data and list it on the DList. Ive now worked out it will be something like this
[lua]local read = trailread
umsg.Start( “Loading Trails”, ply )
for k,v in pairs( read ) do
umsg.String( v.DermaListView:AddLine )
umsg.End
end[/lua]

but I need to make it add the list to the dermalist: DermaListView:AddLine(v)

[editline]05:13PM[/editline]

Okay I now have this in my commands.lua
[lua]function loadtrail(ply,cmd,args)
local read = file.Read(“player_trails/”…string.Replace(ply:SteamID(),":","_")…".txt")
umsg.Start( “loadtrail”, ply )
umsg.Short( #read )
for k,v in pairs( read ) do
umsg.String( v )
end
umsg.End( )
end

concommand.Add(“loadtrail”, loadtrail)[/lua]

and this in my panel
[lua]local DermaListView = vgui.Create(“DListView”, self)
DermaListView:SetPos(15, 160)
DermaListView:SetSize(80, 200)
DermaListView:SetMultiSelect(false)
DermaListView:AddColumn(“Trails”)
loadtrail()

function DermaListView:OnRowSelected( LineID, Line ) 
	-- Line is a DListView_Line panel object 
	name = Line:GetColumnText( 1 )
	RunConsoleCommand("setTrail",name)
end[/lua]

The error im getting is


(...mode\gamemode\osmenu/player_stats.lua:52: attempt to call global 'loadtrail' (a nil value))

Im really stuck now.

Loadtrail should be a console comnand on the server and does not exist as a function on the client.
So you should be using RunConsoleCommand instead of calling it.

[editline]03:25PM[/editline]

Also you might want go be doing an admin check on the playet calling your console command.

[quote=“Crazy Quebec, post:8, topic:38379”]

Loadtrail should be a console comnand on the server and does not exist as a function on the client.
So you should be using RunConsoleCommand instead of calling it.

[editline]03:25PM[/editline]

Okay ive nearly got it working now. In my function how can I make it send the umsg in a format like a column as the saved data is a table.
e.g.
trailname
trailname
trailname

rather than a string and list each one as a new line on my DListVIew ?
Here is my function [lua]
function loadtrail(ply,cmd,args)
local read = file.Read(“player_trails/”…string.Replace(ply:SteamID(),":","_")…".txt")
umsg.Start( “loadtrail”, ply )
umsg.Short( #read )
umsg.String( read )
umsg.End( )
end [/lua]
would i put DermaListView:AddLine(read) on my DListView?