• PlayerDisconnected giving NULL Entity.
    8 replies, posted
hey all, guess im doing something wrong again.. xD currently im trying to save a player when they disconnect using this code: [lua] function fPlayerDisconnect( ply ) updatePlayer( ply ) end hook.Add( "PlayerDisconnected", "playerdisconnected", fPlayerDisconnect ) [/lua] but ply gives me a nice NULL Entity: Dropped MiiChaell from server (Disconnect by user.) [@gamemodes\lolol\gamemode\init.lua:89] Tried to use a NULL entity! does someone have any idea? Thanks in advance :)
IsValid(ply) before line 2.
That shouldn't be a problem, show us the code for the updatePlayer function.
[QUOTE=zzaacckk;29484254]IsValid(ply) before line 2.[/QUOTE] I was unaware the player object in PlayerDisconnected could ever be null
PlayerDisconnected is called before the player entity is removed from the server I think, so it's something wrong with the code of updatePlayer.
Updateplayer func. [lua] function updatePlayer( ply ) DB.Query("SELECT * FROM users WHERE steamid = '" .. ply:SteamID() .. "';", function(data) if data then DB.Query("UPDATE users SET totaltime = " .. math.floor( ply:GetPlayTimeTotalTime() ) .. " WHERE steamid='".. ply:SteamID() .."'") end end) end [/lua] it cant be this one, works perfect in the timer i use to save every 5min. i'm clueless O.o
That's a threaded callback. By the time the query is finished, the player has most likely disconnected and is invalid on the server. [lua] function updatePlayer( ply ) local time = math.floor( ply:GetPlayTimeTotalTime() ) local id = ply:SteamID() DB.Query("SELECT * FROM users WHERE steamid = '" .. id .. "';", function(data) if data then DB.Query("UPDATE users SET totaltime = " .. time .. " WHERE steamid='".. id .."'") end end) end [/lua]
After the player disconnected, their entity becomes invalid. So if you print the player arguent in the disconnect hook it wont be null. So if you use that entity in a timer, when the timer runs the players entity is already removed so it became a NULL entity. There your database query works as a timer because it is not called immediately. Edit: I had to answer the phone :ninja:
Thanks guys, especially raBBish. where would i be without you all :3 haha.. Solved :) plz close
Sorry, you need to Log In to post a reply to this thread.