• Trying to find player's Steam ID returns 'Null'
    12 replies, posted
I receive this error: [code]database.lua:25: Tried to use a NULL entity! 1.SteamID - [C]:-1[/code] From this piece of code: [lua]function ply:ShortenSteamId() local id = ply:SteamID() local id = tostring(id) local id string.Replace(id, "STEAM_0:0:", "") local id string.Replace(id, "STEAM_0:1:", "") return id end[/lua] Here's the whole file if you need it: [lua]local ply = FindMetaTable("Player") util.AddNetworkString( "database" ) local oldPrint = print local function print(s) oldPrint("database.lua: " .. s) end function ply:databaseDefault() self:databaseSetValue( "money", 1000 ) local i = {} i[ "testitem" ] = { ammount = 1 } self:databaseSetValue( "inventory", i ) end function ply:databaseNetworkedData() local money = self:databaseGetValue( "money" ) self:SetNVInt("money", money) self:KillSilent() self:Spawn() end function ply:ShortenSteamId() local id = ply:SteamID() local id = tostring(id) local id string.Replace(id, "STEAM_0:0:", "") local id string.Replace(id, "STEAM_0:1:", "") return id end function ply:databaseFolders() return "server/customrpgamemode/players/" .. ply:ShortenSteamId() .. "/" end function ply:databasePath() return self:databaseFolders() .. "database.txt" end function ply:databaseSet( tab ) self.database = tab end function ply:databaseGet() return self.database end function ply:databaseCheck() self.database = {} local f = self.databaseExists() if f then self:databaseRead() else self:databaseCreate() end self:databaseSend() self:databaseNetworkedData() end function ply:databaseSend() net.Start( "database" ) net.WriteTable( self:databaseGet() ) net.Send( self ) end function ply:databaseExists() local f = file.Exists( ply:databasePath(), "DATA" ) return f end function ply:databaseRead() local str = file.Read( ply:databasePath(), "DATA" ) self:databaseSet(util.KeyValuesToTable(str) ) end function ply:databaseSave() local str = util.TableToKeyValues( self.database ) local f = file.Write( ply:databasePath(), str ) self:databaseSend() end function ply:databaseCreate() self:databaseDefault() local b = file.CreateDir( self:databaseFolders() ) self:databaseSave() end function ply:databaseDisconnect() self:databaseSave() end function ply:databaseSetValue( name, v) if not v then return end if type(v) == "table" then if name == "inventory" then for k, b in pairs(v) do if b.ammount <= 0 then v[k] = nil end end end end local d = self:databaseGet() d[name] = v self:databaseSave() end function ply:databaseGetValue( name ) local d = self:databaseGet() return d[name] end [/lua] Any help would be appreciated.
Don't shorten it, instead replace the ":" with "_".
[QUOTE=brandonj4;40991628]Don't shorten it, instead replace the ":" with "_".[/QUOTE] I can do that, but the error comes from receiving the Steam ID to begin with. Edit: [lua]function ply:ShortenSteamId() local id = ply:SteamID() local id = tostring(id) local id string.Replace(id, ":", "_") return id end[/lua] Still gives me the same error message.
[QUOTE=Zyler;40991639]I can do that, but the error comes from receiving the Steam ID to begin with. Edit: [lua]function ply:ShortenSteamId() local id = ply:SteamID() local id = tostring(id) local id string.Replace(id, ":", "_") return id end[/lua] Still gives me the same error message.[/QUOTE] Don't use tostring() on it, it's already a string. Are you hooking this and/or calling it anywhere else? Use "self" inside the function instead of ply. Fixed function: [lua]function ply:ShortenSteamId() local id = self:SteamID() id = string.gsub(id, ":", "_") return id end [/lua]
You don't know how to use Lua methods, the inside a function the subject is always called "self".
[QUOTE=brandonj4;40991763]Don't use tostring() on it, it's already a string. Are you hooking this and/or calling it anywhere else? Use "self" inside the function instead of ply. Fixed function: [lua]function ply:ShortenSteamId() local id = self:SteamID() id = string.gsub(id, ":", "_") return id end [/lua][/QUOTE] I get the same error when using the fixed function. The only other place the function is called is in the next function in line in that file: [lua]function ply:databaseFolders() return "server/customrpgamemode/players/" .. ply:ShortenSteamId() .. "/" end[/lua] the full error code is: [code]database.lua:25: Tried to use a NULL entity! 1.SteamID - [C]:-1 2. ShortenSteamID - gamemodes/*filepath*/database/database.lua:25 3. databaseFolders - gamemodes/*filepath*/database/database.lua:31 4. databasePath - gamemodes/*filepath*/database/database.lua:35 5. databaseExists - gamemodes/*filepath*/database/database.lua:65 6. databaseCheck - gamemodes/*filepath*/database/database.lua:48 7. unknown - gamemodes/*filepath*/init.lua:9[/code] line 8, 9 and 10 of init.lua: [lua]function GM:PlayerInitialSpawn( ply ) ply:databaseCheck() end[/lua] database.lua: [lua]local ply = FindMetaTable("Player") util.AddNetworkString( "database" ) local oldPrint = print local function print(s) oldPrint("database.lua: " .. s) end function ply:databaseDefault() self:databaseSetValue( "money", 1000 ) local i = {} i[ "testitem" ] = { ammount = 1 } self:databaseSetValue( "inventory", i ) end function ply:databaseNetworkedData() local money = self:databaseGetValue( "money" ) self:SetNVInt("money", money) self:KillSilent() self:Spawn() end function ply:ShortenSteamId() local id = self:SteamID() id = string.gsub(id, ":", "_") return id end function ply:databaseFolders() return "server/customrpgamemode/players/" .. ply:ShortenSteamId() .. "/" end function ply:databasePath() return self:databaseFolders() .. "database.txt" end function ply:databaseSet( tab ) self.database = tab end function ply:databaseGet() return self.database end function ply:databaseCheck() self.database = {} local f = self.databaseExists() if f then self:databaseRead() else self:databaseCreate() end self:databaseSend() self:databaseNetworkedData() end function ply:databaseSend() net.Start( "database" ) net.WriteTable( self:databaseGet() ) net.Send( self ) end function ply:databaseExists() local f = file.Exists( ply:databasePath(), "DATA" ) return f end function ply:databaseRead() local str = file.Read( ply:databasePath(), "DATA" ) self:databaseSet(util.KeyValuesToTable(str) ) end function ply:databaseSave() local str = util.TableToKeyValues( self.database ) local f = file.Write( ply:databasePath(), str ) self:databaseSend() end function ply:databaseCreate() self:databaseDefault() local b = file.CreateDir( self:databaseFolders() ) self:databaseSave() end function ply:databaseDisconnect() self:databaseSave() end function ply:databaseSetValue( name, v) if not v then return end if type(v) == "table" then if name == "inventory" then for k, b in pairs(v) do if b.ammount <= 0 then v[k] = nil end end end end local d = self:databaseGet() d[name] = v self:databaseSave() end function ply:databaseGetValue( name ) local d = self:databaseGet() return d[name] end [/lua] beginning of init.lua: [lua]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) AddCSLuaFile( "database/cl_database.lua" ) include( "shared.lua" ) include( "database/database.lua" )[/lua]
You still didn't replace "ply" with "self" in all of your functions. Put a timer delay for one second inside your "PlayerInitialSpawn" hook.
[QUOTE=brandonj4;40991955]You still didn't replace "ply" with "self" in all of your functions. Put a timer delay for one second inside your "PlayerInitialSpawn" hook.[/QUOTE] Some functions are correct, others are not.
I would call the player meta table "playerMeta" instead of "ply", as "ply" can cause a lot of confusion, since it's normally used as a brief version of "player"
Using Brandon's fixes I get this error: [code]gamemodes/*filepath*/database/database.lua:65: attempt to index local 'self' (a nil value)[/code] line 64, 65, 66 and 67 respectively: [lua]function ply:databaseExists() local f = file.Exists( self:databasePath(), "DATA" ) return f end[/lua] changing 'self:databasePath()' to 'ply:databasePath()' seems to fix this, but it seems to lead to the other error I mentioned in the OP. database.lua: [lua]local ply = FindMetaTable("Player") util.AddNetworkString( "database" ) local oldPrint = print local function print(s) oldPrint("database.lua: " .. s) end function ply:databaseDefault() self:databaseSetValue( "money", 1000 ) local i = {} i[ "testitem" ] = { ammount = 1 } self:databaseSetValue( "inventory", i ) end function ply:databaseNetworkedData() local money = self:databaseGetValue( "money" ) self:SetNVInt("money", money) self:KillSilent() self:Spawn() end function ply:ShortenSteamId() local id = self:SteamID() id = string.gsub(id, ":", "_") return id end function ply:databaseFolders() return "server/customrpgamemode/players/" .. self:ShortenSteamId() .. "/" end function ply:databasePath() return self:databaseFolders() .. "database.txt" end function ply:databaseSet( tab ) self.database = tab end function ply:databaseGet() return self.database end function ply:databaseCheck() self.database = {} local f = self.databaseExists() if f then self:databaseRead() else self:databaseCreate() end self:databaseSend() self:databaseNetworkedData() end function ply:databaseSend() net.Start( "database" ) net.WriteTable( self:databaseGet() ) net.Send( self ) end function ply:databaseExists() local f = file.Exists( self:databasePath(), "DATA" ) return f end function ply:databaseRead() local str = file.Read( self:databasePath(), "DATA" ) self:databaseSet(util.KeyValuesToTable(str) ) end function ply:databaseSave() local str = util.TableToKeyValues( self.database ) local f = file.Write( self:databasePath(), str ) self:databaseSend() end function ply:databaseCreate() self:databaseDefault() local b = file.CreateDir( self:databaseFolders() ) self:databaseSave() end function ply:databaseDisconnect() self:databaseSave() end function ply:databaseSetValue( name, v) if not v then return end if type(v) == "table" then if name == "inventory" then for k, b in pairs(v) do if b.ammount <= 0 then v[k] = nil end end end end local d = self:databaseGet() d[name] = v self:databaseSave() end function ply:databaseGetValue( name ) local d = self:databaseGet() return d[name] end [/lua] I don't think changing the name of the Player Meta Table will fix any of this, a side from making it less confusing to read, as it is called locally in the file.
In ply:databaseCheck(), change this [code] local f = self.databaseExists() [/code] to this: [code] local f = self:databaseExists() [/code] Defining a function using a colon will create an implicit 'self' argument, but in order for it to work correctly, you must either call that function with a colon, or pass the player entity to it manually.
[QUOTE=Zyler;41000559]I don't think changing the name of the Player Meta Table will fix any of this, a side from making it less confusing to read, as it is called locally in the file.[/QUOTE] I wasn't saying that, it was just some advice.
Thanks everyone, it seems to be working now.
Sorry, you need to Log In to post a reply to this thread.