luagarrys-mod

Can't get PlayerSay working in Gmod lua addon


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.


Solution

  • 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()