Else if and hash !Help!

Hi I have a script which I want to optimize as it looks horrid. I wondered if anyone could help me or show me any guides on it.

I want to try and use a ‘hash’ for this script if at all possible.




elseif (text == "!help") then
			pl:PrintMessage( HUD_PRINTCENTER, "Help has been printed in console" )
elseif (text == "!Server") then
			pl:PrintMessage( HUD_PRINTCENTER, "The server rules are located in the f1 menu" )
elseif (text == "!guide") then
			pl:PrintMessage( HUD_PRINTCENTER, "The guide is located in the f2 menu." )



This is just a few examples of what I am working with, if anyone could help me with this I will be very greatful.

Thanks.

-Duby

It’s as simple as using a table then indexing it:
[lua]local tbl = {
[ “!help” ] = “Help has been printed in console”,
[ “!server” ] = “The server rules are located in the f1 menu”,
[ “!guide” ] = “The guide is located in the f2 menu.”,
}

if tbl[ text ] then pl:PrintMessage( HUD_PRINTCENTER, tbl[ text ] ) end[/lua]



local ChatCommands = {}
ChatCommands['!help']     = 'Help has been printed in the console'
ChatCommands['!server'] = 'The server rules are located in the F1 menu'
ChatCommands['!guide']     = 'The guide is located in the F2 menu'

function PlayerCmd( pl, text )

    if ( ChatCommands[ text ] ) then
    
        pl:PrintMessage( HUD_PRINTCENTER, ChatCommands[ text ] )
        return false
    
    end

end
hook.Add( 'PlayerSay', 'PlayerCmd', PlayerCmd )


Crude, but it works, tested it and it’s fine, I’m sure you could adapt it to suit your needs.

There’s probably a better way, but this is how I would do it.

**Edit: **Oh you little shit

I do this for chat-commands: https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html

I just detect the first character; if it is ! or / its a command and calls the console command. ! allows the text to be printed while / is silent.

Make sure you do string.lower( text ) == x to make sure that all cases can be used.

Hmm thankyou guys very much! I will use these techniques to further my knowledge. :slight_smile: