Jamie Bowen
Jamie Bowen

Reputation: 45

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.

Upvotes: 0

Views: 871

Answers (2)

user9622872
user9622872

Reputation:

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.

Upvotes: 0

Piglet
Piglet

Reputation: 28950

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

Upvotes: 0

Related Questions