I want to pass values from server.lua to client.lua

Environment questions

What is the issue you’re having?
send value from server to client is not work.

What are you trying to do?
I want to get the license of a player who has connected to the server, get the charinfo of the players table for that player in the database based on that license, and pass the obtained value to the client.

sorry my english is not good.

What have you tried already to fix the issue?

checked the __resource.lua✅
Checked for RegisterNetEvent matches.:white_check_mark:

Error screenshot (if any):
image

no error on Live Console.
F8 Console have not error too.

What server did you get this issue on?
vps:zap-hosting
Framework:qbcore

----Files----
__resource.lua

fx_version 'cerulean'

games { 'gta5' }

author 'myname'

version '1.0.0'

client_scripts {

	'client/client.lua',
	'config.lua',
}

server_scripts {

	'server/server.lua',
	'@mysql-async/lib/MySQL.lua'

}

client.lua

RegisterNetEvent('SendToClient')
AddEventHandler('SendToClient', function(playerInfo)
   print(playerInfo)
end)

server.lua

AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
    local source = source

    local playerLicense = GetPlayerIdentifier(source, 0)
    local charInfo = GetCharInfoByLicense(playerLicense)
    
    -- print("GetRoalNameFromInfo-->",GetRoalNameFromInfo(charInfo)) <-- this is work
    TriggerClientEvent('SendToClient', source, GetRoalNameFromInfo(charInfo))
end)

function GetCharInfoByLicense(playerLicense)
    local charInfo = nil
    local query = "SELECT charinfo FROM players WHERE license = @license LIMIT 1"

    MySQL.Async.fetchScalar(query, {['@license'] = playerLicense}, function(result)
        if result then
            charInfo = result
        end
    end)

    while charInfo == nil do
        Citizen.Wait(10)
    end

    return charInfo
end

function GetRoalNameFromInfo(charInfo)

    local FirstName = ""
    local LastName = ""

    local InfoData = json.decode(charInfo)

    FirstName = InfoData.firstname
    LastName = InfoData.lastname

    -- new { "firstname":Firstname, "lastname":Lastname}
    local newInfoData = json.encode({
        firstname = FirstName,
        lastname = LastName
    })

    return newInfoData
end

Hello, this is a friendly reminder because this is your first time creating a topic (or it has been a while since your last topic) in this category.

Please note that most of the support is provided by the Cfx.re community on a voluntary basis. We ask you to be patient; there is no guarantee we have a solution to your problem(s). To avoid unnecessary/duplicate topics, please browse the forums before creating a topic.

To improve your chances of your issue(s) being solved, please provide as much information as possible about the issue(s) you are having. Also —whenever possible— please use the template given to you when creating a topic.

Thanks for keeping these forums tidy!
:mascot:

Start by renaming this to fxmanifest.lua.

The key word here is connecting; the player has not yet joined the game:

https://docs.fivem.net/docs/scripting-reference/events/list/playerConnecting/

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.