Change players dimension on first connect - Problem

Hello everyone! I tried to make a script where It would check if a player indetifier exists in the db and if not it will change the players dimension on first connect and only on first connect. I have ran into a problem where the data from the server side of the script is not sent to the client.lua and does not execute. I have no idea why it is not working and I would really appreciate any help.

client.lua :

local firstServerConnect
local playerId = GetPlayerServerId(PlayerId())

RegisterNetEvent('connect:firstConnect')
AddEventHandler('connect:firstConnect', function(firstConnect)
    if firstConnect then 
        firstServerConnect = true
        print('Connect == False')
    else 
        firstServerConnect = false 
        print('Connect == True')
    end
end)

AddEventHandler("playerSpawned", function()
    if firstServerConnect then
        print('test')
        print(playerId)
        print(firstConnectFlag)
        TriggerServerEvent('player:dimenzija', playerId)
    end
end)

server.lua :

--Server Dimenzija del

RegisterNetEvent('player:dimenzija')
AddEventHandler('player:dimenzija', function(playerId)
    SetPlayerRoutingBucket(playerId, playerId)
    local playerDimenzija = GetPlayerRoutingBucket(playerId)
    TriggerClientEvent('player:dimenzijaId', playerId, playerDimenzija)
end)


RegisterNetEvent('player:zamenjavaDimenzije')
AddEventHandler('player:zamenjavaDimenzije', function(playerId)
    SetPlayerRoutingBucket(playerId, 0)
end)

-- Identifier check


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

    print(source)
    local fullIdentifier = GetPlayerIdentifier(source, 0)
    local parts = {}
    for part in fullIdentifier:gmatch("[^:]+") do
        table.insert(parts, part)
    end

    local identifier = parts[2]
    -- Preverba SQL
    MySQL.Async.fetchScalar('SELECT 1 FROM users WHERE identifier = ?', {identifier}, function(result)
        if result then
            firstConnect = false
            print('Obstaja')
        else 
            firstConnect = true
            print('Ne obstaja')
        end

        TriggerClientEvent('connect:firstConnect', source, firstConnect)
        deferrals.done()
        print('Identifier:', identifier)
    end)    
end)

yeah so basically the playerConnecting (as it sounds) is triggered when player is connecting and when the player is connecting it means the scripts aren’t loaded yet - that mean’s the client side cannot receive the event, because it’s not registered yet. If I were you, I would change the playerSpawned event to onlu trigger there a server sided event. In that server sided event I would check the identifier and then set (or not) the routing bucket. I hope I made my self clear and helped you :smiley:

1 Like

You’re doing a lot of ping-ponging around here when none of this is needed, just use the “playerJoining” event on the server, which only gets sent when the client is able to take in events iirc.

-- tempId is the source the player has from 'playerConnecting'
RegisterNetEvent("playerJoining", function(tempId)
    -- implicit source
    local src = source
    SetPlayerRoutingBucket(src, src)
end)
1 Like

Aha I see. Thank you! :heart:

Might be a dumb question, but where should the playerJoining be triggered inside of the playerConnecting to send the data down to it, because when I try putting it in and triggering it while firstConnect is true it still triggers before the player has joined the game and does not change the routing.

AddEventHandler('playerConnecting', function()
    local playerId = source;
    SetPlayerRoutingBucket(playerId, playerId)
    local playerDimenzija = GetPlayerRoutingBucket(playerId)
    TriggerClientEvent('player:dimenzijaId', playerId, playerDimenzija)
end)