Attempt to index a non-table object(nil) error in lua module

I’m getting an error in my Lua match module while parsing the state of the match. I even added a nil check but it doesn’t seem to work . The error I get is

and my code in the lua match module is :

function M.match_join(context,dispatcher,tick,state,presences)
  if not state then state={} end
    for _,presence in ipairs(presences) do
        state.presences[presence.session_id] = presence
    nk.logger_info("match joined")
    return state

I get an error in the line state.presences[presence.session_id] = presence

@shibli_mansuri I suspect you’ve not created your presences field in the state table. I’ll add some detail to some of the example code you shared that might help.

Each match handler in Nakama has a state object that can is kept by the match and can store whatever you like. This is where you store all the game state you want to keep while a match is active on the server. For example in your code you use.

if not state then
    state = {}

This sets your match state to be an empty Lua table if it was nil. After that you attempt to access a table called presences inside the state object: state.presences. You’ll need to make sure that you’ve created the table before you can index into it.

if not state then
    state = {
        presences = {}

Although this will likely solve your error I recommend you initialize the state of the match inside the match_init callback like is done in our code examples:

This way you don’t have to do the nil check in match_join at all. Hope this helps.

Thank you, that worked.