when players join, I retrieve their info from a database (MySQL) and store them to a variable. It seemed to work well until I tried joining the server at the same time with another player. What could be the solutions? PHP: SELECT MONEY, SKIN,FROM pvdb WHERE NAME=:name;-- # }-- # }-- # } PHP: public function load( Player $player, callable $callable ): void { $this->connector->executeSelect( "pbdb.load", [ "name" => $player->getName() ], $callable ); } PHP: public function retrieveMoney ( Player $player ){$balance = [];$this->load( $player, function ( array $data ) use ( $uuid, $player ) { foreach ($data as $_data) { $this->balance[$player->getName()] = $_data[ "MONEY" ] } } );}
Then more players join together, and you're still having the problem. Before executing something, check of player data are ready. If not loaded yet, push them to a queue to execute later. That's what async is supposed to mean.
Googled how to check if mysql data are ready and nothing came up helpful lol can u even check if data are ready
Several ways you can do that. Authentication plugins are a great example. HereAuth f.e completely locks the player (no interacting, no block placing or breaking, no chatting etc) until they type in their password. You can do something like that. Lock the player until their initial data has been fetched from the database. PHP: public function load(Player $player){ lock($player); $this->connector->executeSelect($stmt, $params, function() use($player){ unlock($player); });}// Listen to PlayerInteractEvent, BlockBreakEvent etc at a low priority// and cancel if isLocked($player);