Created
November 19, 2023 13:39
-
-
Save DemmyDemon/7e9b8ff123b72856a7eb6ca62ee1d4c8 to your computer and use it in GitHub Desktop.
Deferral and database lookup of player (entirely untested, educational purposes only)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals) | |
-- First, a local copy of source, so it doesn't get lost while deferring. | |
local source = source | |
-- Actually defer the connection | |
deferrals.defer() | |
-- Wait a tick, so the deferral happens. | |
Citizen.Wait(0) | |
-- Tell the client what we're doing, in case it stalls at this point, somehow. | |
deferrals.update('Updating player name...') | |
-- Grab the identifier we need. | |
local identifier = GetPlayerIdentifierByType(source, 'license') | |
-- Query the database | |
MySQL.Async.insert('INSERT INTO Data (identifier, spelernaam) VALUES (@identifier, @spelernaam) ON DUPLICATE KEY UPDATE spelernaam = @spelernaam', { | |
['@identifier'] = identifier, | |
['@spelernaam'] = playerName | |
}, function(rowsAffected) | |
if rowsAffected > 0 then | |
--[[ This makes no sense, as the player's chat resource isn't running yet. | |
TriggerClientEvent('chat:addMessage', source, { | |
args = {'Your data has been processed.'} | |
}) | |
--]] | |
deferrals.done() -- Admit the player. | |
else | |
-- Failure, so we deny the player. | |
deferrals.done('Could not update player name, or whatever') | |
print('Error processing data for ' .. playerName) | |
end | |
end) | |
end) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment