when is it safe to store a player's entity VS storing their SteamID?

Well obviously you should store their SteamID when saving player data across sessions (like in files, mySQL, etc),
but what about during one game session?

I have a table where I’m storing players who press a button, so I can reference them later.
In this case, is it safe to just store their player entity, or could player entity indices get mixed around when a new player joins?

Storing the player entity reference is fine, just so long as you’re okay with them not being recognized if they were to disconnect and come back. If you store entity references you should check if they are IsValid(plyEnt) before using the references in code; disconnected players are nil.

[editline]2nd February 2016[/editline]

You could just have a table of tables; { {ply1:Nick(), ply1:SteamID()}, {ply2:Nick(), ply2:SteamID()} }

this way you could store steamIDs with the player’s name attached

player.GetBySteamID

use that to find the player entity if they are still in the server

Thank you, that’s pretty much what I was wondering.
I am removing players from the reference table when they disconnect, and validating them before using them in code, so it should all be fine then :>



local allPlayers = player.GetAll()
local allPlayersSafe = {}
for k, v in pairs(allPlayers) do
     local subtable = {v:Nick(),v:SteamID()}
     table.insert(allPlayersSafe,subtable)
end

for k, v in pairs(allPlayersSafe) do
     print("name: "..v[1]..", steamID: "..v[2])
     if player.GetBySteamID(v[2]) then print("player is still connected") else print("player is not still connected") end
end


[editline]2nd February 2016[/editline]

fixed code

[editline]2nd February 2016[/editline]

fixed again

[editline]2nd February 2016[/editline]

fixed a third time; the parser on facepunch hilighted it wrong and confused me

[editline]2nd February 2016[/editline]

anyone know why the facepunch code hilighter thinks the string in the first print doesn’t include the steamID: part?

Doing below is less efficient because it has to cycle through all the players

I recommend just storing them as entities and using IsValid, as said further below.

The point is that if you wanted to display information on potentially disconnected players, it would need a more concrete method of storing the players.

That could be useful for other people browsing the forums with the same question, so I appreciate the effort :slight_smile:

For my code I think just storing the entities is enough, since I just need to know which players to respawn at a certain location at a certain time, but don’t need any additional info.