To remove the default spawn selector in QBox and automatically spawn a player at their last position after selecting a character, follow these steps:
1. Disable the Default Spawn Selector
The default spawn selector is provided by the qbx_spawn resource. You can disable this by editing the server.cfg and removing or commenting out the line that starts this resource.
Open your server.cfg file and find this line:
start qbx_spawn
Comment it out or remove it entirely:
# start qbx_spawn
2. Save Last Position to the Database
Ensure that the player's last position is saved in your database. QBox already supports saving player positions, so it might already be handled by your server. The player's position is usually stored in a field like position within the character data.
If this is not handled by default, you'll need to ensure that the player position is saved in the database whenever they log out or disconnect. Use the following server-side event to save the player's position:
AddEventHandler('playerDropped', function()
local player = source
local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(player)))
end)
3. Automatically Spawn Player at Last Position
Once the position is saved, you need to spawn the player at their last position when they log back in. You can do this by retrieving their last saved position from the database and setting their spawn point. Modify the qbx_core login process by hooking into the exports.qbx_core:Login() and adding a position check.
Here is an example of how to spawn the player at their last saved position after character selection:
AddEventHandler('qbx_core:playerLoaded', function(player)
local lastPosition = exports.oxmysql:scalarSync('SELECT position FROM characters WHERE citizenid = ?', {player.PlayerData.citizenid})
if lastPosition then
local pos = json.decode(lastPosition)
SetEntityCoords(GetPlayerPed(player.source), pos.x, pos.y, pos.z, false, false, false, true)
else
SetEntityCoords(GetPlayerPed(player.source), 0.0, 0.0, 72.0, false, false, false, true)
end
end)
4. Ensure the Position is Saved on Character Switch
To ensure the player's position is correctly saved when they switch characters or log out, ensure the last known position is saved at those moments. You can add this logic to your character logout or switch handler using:
AddEventHandler('qbx_core:Logout', function(player)
local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(player.source)))
exports.oxmysql:execute('UPDATE characters SET position = ? WHERE citizenid = ?', {json.encode({x = x, y = y, z = z}), player.PlayerData.citizenid})
end)
By following these steps, the server will no longer use the default spawn selector, and players will automatically be spawned at their last saved position after selecting a character.