0

I'm learning to code and using a game and I made this code where when a player joins his vehicles stored in the database are spawned. The problem is that they're spawned in a function (I don't know any other way to do it) and I want them to be destroyed when he leaves, but I can't access them. Is there anyway to access variables stored in a function from outside? Or any other way to do this? Maybe an event?

The code:

mp.events.add("playerJoin", (player) => {
    var socialID = player.rgscId;
    //[IRRELEVANT CODE]
            con.query("SELECT vehicleID, vehicleModel, vehicleSpawnX, vehicleSpawnY, vehicleSpawnZ FROM vehicles WHERE vehicleType = ? AND vehicleOwner = ?", ["players", playerID], function (err, result) {
                if (err) console.log(err)
                for (i = 0; i < result.length; i++) {
                    vehicleModel = result[i].vehicleModel
                    //[THE VEHICLE IS CREATED IN THE LINE BELOW]
                    result[i].vehicleID = mp.vehicles.new(parseInt(vehicleModel), new mp.Vector3(result[i].vehicleSpawnX, result[i].vehicleSpawnY, result[i].vehicleSpawnZ),
                }
            });
        });
        con.release()
    });
});

How can I access the variable (result[i].vehiceID) from outside the function or do this any other way? With an event? Let's say I added a custom outside event where I'd define the variables (names of the vehicles) and then I'd call that event once the player joins. Would those variables be global? THanks!

  • Asynchronous coding is confusing for experienced programmers. If you're a beginner, you probably should start on something simpler. – Barmar Sep 08 '20 at 20:23
  • Thanks. Then I want to find another way of doing this, so please make my question available again. I said nothing about asynchronous coding. –  Sep 08 '20 at 20:27
  • You probably can't do this without asynchronous code. – Barmar Sep 08 '20 at 20:31
  • AFAIK, there's no synchronous MySQL access. – Barmar Sep 08 '20 at 20:32
  • Not mentioning asynchronous coding, doesn't mean you are not writing asynchronous code. – Taplar Sep 08 '20 at 20:33

0 Answers0