Making a simple steam connection identifier (Learning Lua)

Hello soo i’m wondering why am i getting an error while debugging this code? also if there’s anything wrong, would anybody care to explain?

I know there are plentys out there but i’m trying to learn lua soo … yeah.

Also, am i supposed to register the event? if so, why is that?

addEventHandler('playerConnecting', function(playerName, setKickReason))    
    local identifySteam = string.sub(GetPlayerIdentifiers(source), 1, 5) -- Grabbing the 5 letters from the steam id "steam".
    
    if identifySteam == steam then -- Comparing the 5 letters with the word steam and if they are correct then i'm allowing the user to connect.
      deferrals.done() 
    else
      deferrals.done("You're not using a steam account.") -- If they don't match, i'm kicking him out.
    end 
  end

Apparently if you call out deferrals.done() it’s supposed to allow the connection but if you put in a label inside the parentesis it’ll kick the user.

I’ve done this code with the wiki, is this info accurate?

https://wiki.fivem.net/wiki/Event:PlayerConnecting

AddEventHandler not addEventHandler, you are also missing the deferrals parameter in the function. You are also comparing identifySteam to a nil variable, it should be identifySteam == "steam".

Alright @Nick78111 i fixed it.

It looks like this but now it’s failing to load in?

AddEventHandler('playerConnecting', function(playerName, deferrals))    
    local identifySteam = string.sub(GetPlayerIdentifiers(source), 1, 5)
    
    if identifySteam == "steam" then
      deferrals.done() 
    else
      deferrals.done("You're not using a steam account.") 
    end 
  end

Should be function(playerName, setKickReason, deferrals)

You also have to call deferalls.defer()

AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals))
    local identifySteam = string.sub(GetPlayerIdentifiers(source), 1, 5)
    
    if identifySteam == "steam" then
      deferrals.done() 
    else
      deferrals.defer("You're not using a steam account.") 
    end 
  end

Still getting unexpected symbol near ) line 1 but i’m not sure what is wrong.

Do i have to register the event?

Okay for some reason i had to close the parenthesis when the function ended (at the last end).

Yeah that’s how it works. It’s meant to be like that.

AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
    local identifySteam = string.sub(GetPlayerIdentifiers(source)[1], 1, 5)
    
    if identifySteam == 'steam' then
        deferrals.done()
    else
        deferrals.defer('You are not using a steam account.')
    end
end)

Something like that, but I am not experienced with deferrals.

You probably want to defer the connection first, wait a tick and then check if it’s done or has an error:

AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
    -- Tell the system we want to defer this connection
    defferals.defer()

    -- Wait for the next server tick, this is required.
    Citizen.Wait(0)

    local identifySteam = string.sub(GetPlayerIdentifiers(source)[1], 1, 5)
    
    if identifySteam == 'steam' then
        -- We have a steam id, let the player connect.
        deferrals.done()
    else
        -- We have not found a steam id, tell the user that it failed to fetch a steam id.
        deferrals.done('You are not using a steam account.')
    end
end)
1 Like

This is really old but you spelt deferrals wrong once :stuck_out_tongue: