I use this query to fetch all players with group Superadmin:
local result = MySQL.Sync.fetchAll(“SELECT identifier FROM users WHERE group = ‘superadmin’”)
Now I want to send a message to all people included in this query, but I need to grab their ID. How can I do it?
What type of message do you want to send? Is this a resource-specific thing (e.g. esx)?
It’s an event message of a chat resource, but that’s not important. I just would like to know how to grab their id
The player’s ID or their database user ID? I’d assume the former, as it looks like the identifier column you’re pulling out in your query is their user ID, right? If that’s not the user ID, then you need to specify the correct column in your query. The link @qq_strike posted would help with that.
A player’s ID is different every time they login. When they login, you’d need to keep track of their user ID and their player ID, using something like a dictionary. Then you’d be able to get which player ID maps to the user ID.
You could even use a StateBag or Decor to store the information on the player itself.
The identifier is something to identify them outside fivem. Like their social club id or steam or whatever. You want to translate this to the id fivem is using. So you have to either loop through all connected player and check their id.
Or save an identifier and their id when a player joins and later use this information.
If you are using ESX for example you can di this. ESX.GetPlayerByIdentifier I think is the correct one. The retuned player will have a field called source which is the ID you are searching for. Other tools may have other methods of retrieving.
1 Like
local result = MySQL.Sync.fetchAll("SELECT identifier FROM users WHERE `group` = 'superadmin'")
TriggerClientEvent('chat:addMessage', HERE, {
template = '<div class="chat-message"> - <b> aaaaaa </b> </div>',
})
CancelEvent()
Where I wrote “HERE” I should put the user to send the message
Yes, I totally understand what you want and my explanation will give you exactly what you need.
Like I said you either use a tool like ESX, or vRP or whatever. Or you have to get the id yourself. If you do not have a framework in place and you do not want to create a lookup table with the data on Player join, your best bet is I think to loop through all players connected and send them a message.
I will write this in here, like always. So keep an eye open for typos:
local result = MySQL.Sync.fetchAll("SELECT identifier FROM users WHERE `group` = 'superadmin'")
if result then
local lookupTable = {} -- I create a temp lookUpTable, because its the best datastructure for the job
for _, playerId in ipairs(GetPlayers()) do
lookupTable[GetPlayerIdentifiers(playerId)[1]] = playerId -- check this. I believe GetPlayerIdentifiers(playerId)[1] is the identifier from your database
end
for k,v in pairs(result) do
TriggerClientEvent('chat:addMessage', lookupTable[v.identifier], {
template = '<div class="chat-message"> - <b> aaaaaa </b> </div>',
})
end
end
CancelEvent()
I’m getting this error:
[ script:chat] SCRIPT ERROR: Execution of native 000000002f7a49e6 in script host failed: Argument at index 1 was null.
Always brings up a server id when triggering a client event
if you’re going to send that event to the guy who triggered this server event ,use source for server id
if you’re going to send that event to every player in server ,use -1 for server id
e.g.
TriggerClientEvent(‘event_name’ ,source)
TriggerClientEvent(‘event_name’ ,-1)
Yeah. We need to filter for player that are actual online. My bet would be that TriggerClientEvent throws that exception, because lookupTable[v.identifier] has no entry for players not connected.
Add a if lookupTable[v.identifier] then around TriggerClientEvent. This should do the trick
you can use GetPlayerFromIdentifier to detect the player is online or not