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!