PlayerDisconnected is unreliable?

My players seems to report on these random failures in saving their props on exit, while they get timed out or sometimes even when retrying.

I was wondering is it unreliable or is it most likely something in my code that would cause that?

Although I’m not sure on PlayerDisconnected, I know the gameevent.Listen player_disconnect is really reliable.

I don’t think it fits my needs since I don’t believe you can get the player’s entity.

You get their name, SteamID, UserID, bot status, an disconnect reason, which should be enough for what you’re doing.

[editline]28th June 2014[/editline]

Plus, it’s shared.

Game-Events
https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/tracking_players/player_connect_and_disconnect.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/tracking_players/player_connect_and_disconnect_with_list_example.lua.html

These are the data-points


	local _name = _data.name;			// Same as Player:Nick( );
	local _steamid = _data.networkid;	// Same as Player:SteamID( );
	local _id = _data.userid;			// Same as Player:UniqueID( );
	local _bot = _data.bot;				// Same as Player:IsBot( );
	local _reason = _data.reason;		// Text reason for disconnected such as "Kicked by console!", "Timed out!", etc...

You can use a search for name, SteamID, UniqueID to get the entity. Better yet, if you look at the _with_list_example, you’ll see how to manage a list using SteamID as key ( just, in the list, add on PlayerInitialSpawn a reference to the Player Entity or EntIndex, and on Disconnect reference the Entity, or use Entity( EntIndex ) to get the Player Entity through the use of the managed list ).

Well it will be enough for saving their props, but if I am moving to that there is actually quite a few things in my PlayerDisconnected hook that I cannot think of any way to do with just his SteamID such as checking if he ran for president and etc.

How are you currently storing it?

Well say you are arrested so it’s the player entity saving inside the Arrested bool
[lua] Player.Arrested = true/false [/lua]

Now if required I can say make a server-side table and save arrested players there and everything can be changed to work on that hook but that requires literaly remaking half the gamemode so I rather do it only if it’s 100% certain that PlayerDisconnected is really that unreliable.

If that value is clientside, then you can store it straight to the data table.

No need to over-engineer a simple problem. player_disconnect event has a ‘userid’ field which can be passed as an argument to the Player function in order to grab a player object, like this:



gameevent.Listen( 'player_disconnect' )

local function MyPlayerDisconnect( eventData )
	local ply = Player( eventData.userid )
	
	-- Just in case
	if not IsValid( ply ) then return end

	-- Do whatever
end
hook.Add( 'player_disconnect', 'poop', MyPlayerDisconnect )


The player object is still valid when the player_disconnect event occurs so you can safely access all variables stored in it and call functions on it.

I guessed since it doesn’t pass an entity it is called after the player’s entity was already removed.

I will check your code thanks dude.

Edit: Seems to work, however is it reliable and will always return a player’s entity?

Yes, it is reliable and will always return a player’s entity.

Just checked it for an hour or so seems like that. Thanks dude stay awesome.

Sorry for the bump guys but it seems like player_disconnect is unreliable aswell
Just a log I put out on the day to check if the entity is valid on player_disconnect
[lua]
true
true
true
true
true
true
true
false
true
true
true
true
false
true
true
false
true
true
false
false
true
true
true
false
true
false
true
false
true
true
true
true
true
true
true
false
true
[/lua]

And it goes on and on but you get the point.

I’ve tried this and you don’t get a valid player entity if they disconnect before fully joining the server.

Only then will this return false? i’ll have to test that.



hook.Add( "EntityRemoved", "PlayerDisconnected", function( ent )
	if ent:IsPlayer() then
		print( ent:Nick() .. " has left." )
	end
end )


That can work.

Won’t work for players who disconnect whilst joining.

I don’t understand what you want. You were complaining that the object would sometimes be ‘nil’ when they didn’t fully connect, now you say it’s what you need?

Take your pick, you have two good solutions now.

Note: I am not OP.