local ESX = exports["es_extended"]:getSharedObject()
local Players = {}
lib.callback.register('qb-spawn:server:getOwnedHouses', function(source)
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then return end
local houses = MySQL.query.await('SELECT * FROM properties WHERE has_access LIKE ?', {'%'..xPlayer.identifier..'%'})
return houses
end)
RegisterNetEvent('brave_spawn:start')
AddEventHandler('brave_spawn:start', function()
local xPlayer = ESX.GetPlayerFromId(source)
if Players[xPlayer.identifier] == nil or os.time() - Players[xPlayer.identifier] > 4200 then
TriggerClientEvent('qb-spawn:client:setupSpawns', xPlayer.source, true)
elseif os.time() - Players[xPlayer.identifier] < 4200 then
TriggerClientEvent('qb-spawn:client:setupSpawns', xPlayer.source, false)
end
end)
AddEventHandler('esx:playerDropped', function(playerId)
if not ESX.IsPlayerLoaded then return end
local xPlayer = ESX.GetPlayerFromId(playerId)
Players[xPlayer.identifier] = os.time()
end)
server.lua
can you make only firstt login trigger, but if player relog the server will not appear so it will appear once
Assistant
To modify your script so that the spawn setup only triggers on the first login of a player, you can add a persistent storage mechanism to keep track of whether a player has already logged in. In this case, you can use an in-memory table or a database to store this information.
Assuming you want to use a simple in-memory approach, here’s an updated version of your code that triggers the spawn setup only on the first login. You will need to ensure that you clear the player's entry from the Players table when they drop to signify they can log in again as a new session.
Here's how you can do it:
lua
local ESX = exports["es_extended"]:getSharedObject()
local Players = {}
lib.callback.register('qb-spawn:server:getOwnedHouses', function(source)
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then return end
local houses = MySQL.query.await('SELECT * FROM properties WHERE has_access LIKE ?', {'%'..xPlayer.identifier..'%'})
return houses
end)
RegisterNetEvent('brave_spawn:start')
AddEventHandler('brave_spawn:start', function()
local xPlayer = ESX.GetPlayerFromId(source)
-- Check if this player has already triggered spawn setup
if Players[xPlayer.identifier] == nil then
-- If this is their first login, trigger the setup and mark them as having logged in
TriggerClientEvent('qb-spawn:client:setupSpawns', xPlayer.source, true)
Players[xPlayer.identifier] = true -- Mark the player as logged in
else
-- If they have logged in before, do not trigger the setup again
TriggerClientEvent('qb-spawn:client:setupSpawns', xPlayer.source, false)
end
end)
AddEventHandler('esx:playerDropped', function(playerId)
if not ESX.IsPlayerLoaded then return end
local xPlayer = ESX.GetPlayerFromId(playerId)
if xPlayer then
-- Remove the player from the Players table on drop to allow for a new session on next login
Players[xPlayer.identifier] = nil
end
end)
Changes Made:
Tracking First Login: The Players table is used to store whether the player has already executed the spawn setup (only triggers true for first login).
Handling Player Drop: When a player drops, their identifier is removed from the Players table, allowing them to trigger the spawn setup again when they log in next.
Note:
This solution uses in-memory storage, which will reset if the server restarts. If you need this information to persist across server restarts, you may want to consider storing it in a database (like an SQL table that marks if a player's initial login setup has been done). This implementation assumes that player information shouldn't persist after they disconnect.