Common Code Snippets Console
From GMod Wiki
Contents |
Adding a Console Command
local function fPrintSomething( ply, command, arguments ) Msg( "Arguments: " ) PrintTable( arguments ) end concommand.Add( "PrintSomething", fPrintSomething )
Searching for Text in Chat
Script for searching for a string in chat then printing it to the chat area of that client.
function playerSaid( ply, saywhat ) local playerName = ply:GetName() local ip = ply:IPAddress() if string.find(saywhat, "!ip") == 1 then -- Look for !ip in the player's chat -- and make sure it's the first in the sentence. ply:PrintMessage( HUD_PRINTTALK, "IP for " .. playerName .. ": " .. ip .. "." ) end end hook.Add ( "PlayerSay", "playerSaid", playerSaid )
If the string is the separator then it will make you suicide
You can also do it this way ( i use it alot )
hook.Add("PlayerSay", "WikiExample", function( ply, text ) // start the hook local search_word = "!suicide" local sep = string.Explode( " ", text ) /* /---------- EXPLINATION ----------------------------------\ | everytime Lua spots a space, it chops there, | | so if i said "Gmod 10 Is cool" and exploded it | | by " ", it would give me "Gmod", "10" , "is" and "cool" | | after i will put it into a table, and check if the first| | word found is the one we are looking for | \---------------------------------------------------------/ */ if sep[1] == search_word then ply:Kill() end end)
Make a toggled console command
Make a console command that, when bound to a key, can be hooked to one function when pressed, and hooked to another when released.
function DoStuff() // Stuff to do when the key is pressed here end concommand.Add("+dostuff", DoStuff) function StopDoingStuff() // Stuff to do when the key is released here end concommand.Add("-dostuff", StopDoingStuff) ConCommand("bind", "<key>", "+dostuff") // Use in-game console instead. 'bind x +dostuff'. Replace 'x' with the wanted key.