Reading rom a flat file

Hey , i am want to make a custom model thing , where if you are not in the flat file , you will not have a custom model.

so i want the flat file like this:

Steam:ID Model

but the only thing is , i dont know how to asociate the steam id by the model that folows.

this is the code for finding each steam ID , but i still dont know how to make it work

function CustomModel(ply , model)
steamids = string.Explode( "
", file.Read(“custommodels/people.txt”) )
model = string.Explode( " ", file.Read(“custommodels/people.txt”) )
for k,v in pairs(steamids) do
if self:SteamID() == v then
v:SetModel(model)
return true
end
end
return false
end

but that doesnt work.

You could get them into table pairs by doing:

[lua]
local custommodels = {}
local lines = string.Explode(’
‘, file.Read(“custommodels/people.txt”))
for k,v in pairs(lines) do
local exploded = string.Explode(’ ', v)
table.insert(custommodels, {exploded[1], exploded[2]})
end
[/lua]

Now custommodels[1][1] will be a SteamID and custommodels[1][2] will be the model name or whatever. Do PrintTable(custommodels) if you don’t understand the structure.

Even though that method would work, I would be more inclined to use glon.

Yes, glon would be far better. Research that if you don’t want human readable and editable files. Because if you load it that way, you have to write it to a file that way too.

Glon and Explode are both excessive for this.



local k, id, mdl, txt

CustomModels = { }

txt = file.Read( "your/custom/models/file.txt" )

for k in txt:gmatch( "%C+" ) do
  id, mdl = k:match( "(%C*)|(%C+)" )

  if id and mdl then
    util.PrecacheModel( mdl )
    CustomModels[ id ] = mdl
  end
end


Saving is simple as well



local res, k, v

res = ""

for k, v in pairs( CustomModels ) do
  res = string.format( "%s%s%s|%s", res, res ~= "" and "
" or "", k, v )
end

file.Write( "that/path/from/earlier.txt", res )


Untested, but that should do the job.

**[Util.TableToKeyValues

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Util.TableToKeyValues)**
**[Util.KeyValuesToTable

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Util.KeyValuesToTable)**

Old school way >:|

Sucks because it turns all numeric keys and values into strings, and for various other reasons.