05-13-2015, 07:14 AM
(This post was last modified: 05-13-2015, 07:27 AM by Shadowraix.)
(05-13-2015, 06:40 AM)xoft Wrote: Due to multithreading it cannot be so simple - the "player disconnecting" will be processed by a different thread running in parallel to your plugin, so the Player object can become invalid right after the IF has checked it to be valid, but before the --code executes:
-- Player is currently connected, PlayerObj is a valid object if (PlayerObj) then -- Player disconnects right here -- PlayerObj becomes invalid PlayerObj:SendMessage("...") -- fails - PlayerObj invalid, despite the condition endThis is the price we pay for being multithreaded - we do need to use the DoWith and ForEach calls. Those guarantee that the object is valid as long as the callback is executing (but, again, you cannot store the object to be used later after the call).
I see, what about using pcall? This way your code won't break the whole script and if it errors out anywhere in it, it can be handled? If used rarely then I can see it working fine. I know pcall is relatively slow compared to regular function calls.