1

I am following a tutorial on Lua, specifically for making a gamemode in the game Garry's Mod. I've been looking at this for a while and I simply can't find what's wrong.

function ply:databaseFolders()
   return "server/example/players/" .. self:ShortSteamID() .. "/"    --ref. A
end


function ply:databasePath()
   return self:databaseFolders() .. "database.txt"    --ERROR line here, goes up
end


function ply:databaseExists()
   local f = file.Exists(self.databasePath(), "DATA")    --goes up here
   return f
end


function ply:databaseCheck()
   self.database = {}
   local f = self:databaseExists()     --goes up here
   ...
end


function GM:PlayerAuthed(ply, steamID, uniqueID)
   ply:databaseCheck()                                         --goes up here
   print("Player: " .. ply:Nick() .. " has gotten authed.")
end

Summary of code: I want to create a database.txt file at the directory above.

Edit1: When all players leave the game, ref. A is reached, but no file created in directory.

cid
  • 447
  • 2
  • 7
  • 15
  • 1
    possible duplicate of [Attempt to index local 'self' (a nil value)](http://stackoverflow.com/questions/14068944/attempt-to-index-local-self-a-nil-value) – Tom Blodget Aug 10 '14 at 16:05

1 Answers1

2

When you are calling the function databasePath, you are not using the OOP syntax; and therefore self is not implicitly passed to the function. Henceforth, the error. Change the following:

function ply:databaseExists()
   local f = file.Exists(self:databasePath(), "DATA")
   -- notice the use of ---> : <--- here
   return f
end
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Thank you so much!! Yes, this worked. The ':' versus '.' is strange for me since this is my first time encountering it. – cid Aug 10 '14 at 02:51
  • @cid: The `:` syntax for method calls a Lua-specific quirk. There are [good reasons](http://lua-users.org/wiki/ColonForMethodCall) its like that though. – hugomg Aug 10 '14 at 04:08
  • it's actually similar in python – johannes_lalala Nov 13 '14 at 11:23