无法让 PlayerSay 在 Gmod lua 插件中工作

Can't get PlayerSay working in Gmod lua addon

我尝试制作 Garry 的 Mod lua 文件来查找开头包含“/discord”的消息,并将该消息另存为同一目录中的文本文件,我我不熟悉 lua 文件,所以我不确定语法但是当我查看控制台时,没有任何反应,当我查看服务器命令行时,没有任何反应并且没有创建新文件,我什至搜索了我的整个PC.

我使用了 Garry 的 mod wiki 上的以下页面:https://wiki.garrysmod.com/page/GM/PlayerSay 那里给出的代码有效,但是一旦我添加任何内容,它就完全停止工作。这是我的代码:

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)

如有任何帮助,我们将不胜感激。

您不能在 Gary 的 mod 中使用 Lua 的 io 库。请改用 Gary Mod 的文件 module。

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

示例:

local f = file.Open( "cfg/mapcycle.txt", "r", "MOD" )
print( f:ReadLine() )
print( f:ReadLine() )
print( f:Tell() )
f:Close()

关于 Lua 需要注意的一点是 它的数组从索引 1 开始,这也是它成为一种相当古怪的语言的原因。您需要在 1 到 8 之间进行检查以获取您的标签;这应该可以帮助您开始@Piglet 对文件 IO 的实现。

祝你好运,改装愉快!