Help with Tables

Note: I am a Mushclient user, but I think a lot of Mudlet info transfers over.

I would like to have a local table within a trigger. For example,

local Entered = {
Bill,
Bob,
Frank
}

And then I would like a sound to fire off of the information within that table. For example,

Bob has entered.

I would hear a sound indicating that someone on that list has entered, but not if someone not on the list has entered.

Jack has entered.

Any help would be much appreciated.


Comments

  • edited October 2020
    in lua there's a table.contains function, you would do something like

    if table.contains(Entered, matches[2]) then
          playsound()
    end

    Where matches[2] is the regex match from the trigger. Hopefully you can find something in whatever language mushclient uses


  • if you don't mind it throwing errors, you can just do:
    if Entered[matches[2]] then playsound() end

    It will throw an error on null (which will be every entry that isn't in the table) but mostly that doesn't matter since you only care when it does match.
  • edited October 2020
    Accipiter said:
    if you don't mind it throwing errors, you can just do:
    if Entered[matches[2]] then playsound() end

    It will throw an error on null (which will be every entry that isn't in the table) but mostly that doesn't matter since you only care when it does match.
    Literally everything in this post is incorrect.

    They have an ordered list, not a dictionary, so Entered[matches[2]] won't work because there are no keys that are any player names (your keys would be 1, 2 and 3 in the example table). You would need to change the table declaration to Entered = {Bill = true, Bob = true, Frank = true} to do it that way.

    Attempting to access an entry that doesn't exist in a table simply returns nil, but that won't result in an error in that code. In Lua, nil is considered a "false-y" value, which means your if statement will just not execute its body. You'd only get an error from that code if Entered itself wasn't declared, and therefore nil.

    You should really always care about errors. Having easily avoidable errors (which, again, won't be the case with your code anyway) just clutters your debugging unnecessarily, and makes it harder to identify other issues in your code.
  • Antonius is correct. I was misremembering some stuff since pairs iterates over keys in a somewhat random way and I kinda jumped into that and never learnt about basic tables I guess.
Sign In or Register to comment.