1

I have tried making a Garry's Mod lua file to look for messages containing "/discord" at the beginning of them and save that message as a text file in the same directory, I'm not familliar to lua files so I am unsure of syntax but when I look at console, nothing happens, when I look at the server command line, nothing happens and no new file is created, I even serached my entire PC.

I used the following page on the Garry's mod wiki: https://wiki.garrysmod.com/page/GM/PlayerSay and the code given there works but as soon as I added anything, it stopped working completely. Here is my code:

hook.Add( "PlayerSay", "GmodToDiscord", function( player, text, team )
    if ( string.sub( string.lower( text ), 0, 7 ) == "/discord" ) then -- Makes message lowercase to be read by the program.
        local file = io.open("message.txt", "w") -- Opens a text file in write mode.
        file:write(message) -- Pastes in the message.
        file:close() -- Closes the text file.
    end
end)

Any help would be greatly appreciated.

Jamie Bowen
  • 55
  • 1
  • 8

2 Answers2

1

You cannot use Lua's io library within Gary's mod. Use the Gary's Mod's file module instead.

https://wiki.garrysmod.com/page/file/Open

Example:

local f = file.Open( "cfg/mapcycle.txt", "r", "MOD" )
print( f:ReadLine() )
print( f:ReadLine() )
print( f:Tell() )
f:Close()
Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Thanks for the help, but the script still doesn't work when I comment out lines 4 to 6 and replace it with something like ```print ("test")``` – Jamie Bowen Oct 22 '19 at 14:26
0

A thing to note about Lua, and what makes it a rather whacky language, is that it's arrays begin at index 1. You will need to check between 1 and 8 to get your tags; that should help you get started on @Piglet's implementation of the file IO.

Good luck, and happy modding!

James Whyte
  • 788
  • 3
  • 14
  • 1-based indexing doesn't make Lua a "whacky language"... there are plenty of publications on why either of one is better and none of them really convinced me. – Piglet Nov 04 '19 at 07:23