【问题标题】:Lua Problems With GMOD Custom Chat ScriptGMOD 自定义聊天脚本的 Lua 问题
【发布时间】:2017-12-26 23:12:23
【问题描述】:

因此,我目前正在尝试创建一个小脚本,用于当玩家在 GMOD 中输入“!内容”时,例如,他们将通过一个窗口呈现,该窗口将引导他们进入该服务器的 Steam 内容。对于前面的示例它有效,因此我尝试复制模板并更改函数名称等。我没有遇到与所示示例相同的结果,但似乎没有发生任何事情,我不知道为什么,因为我只有更改了函数名称和字符串。如果你能帮助我,那就太好了。提前致谢。

Steam 群聊脚本(作品)

function steamgroupCommand( ply, text)  
    if string.sub( text, 1, 6 ) == "!steam" then  
    ply:PrintMessage( 3, "It Worked!" )  
    ply:SendLua([[gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")]])  
    for k, v in pairs(player.GetAll()) do v:ChatPrint( "Player " .. ply:Nick() .. " has used !steam to view our community Steam Group!" )  
    end  
    end  
end  
hook.Add( "PlayerSay", "Chat", steamgroupCommand )

Discord 聊天脚本(不起作用)

function discordCommand( ply, text)  
    if string.sub( text, 1, 8 ) == "!discord" then  
    ply:PrintMessage( 3, "It Worked!" )  
    ply:SendLua([[gui.OpenURL("https://discord.gg/35rQxcE")]])  
    for k, v in pairs(player.GetAll()) do v:ChatPrint( "Player " .. ply:Nick() .. " has used !discord to view our official Discord server!" )  
    end  
    end  
end  
hook.Add( "PlayerSay", "Chat", discordCommand )

【问题讨论】:

  • 我忘了添加这个文件位于\garrysmod\lua\autorun
  • 你有多个钩子吗?您似乎对两者都使用了相同的标识符;每个标识符只能有一个钩子函数:wiki.garrysmod.com/page/hook/Add

标签: lua garrys-mod


【解决方案1】:

如果您查看documentation of hook.Add,您将看到有关所需参数的详细信息。

第二个参数对你很重要

  1. any标识符

唯一标识符,通常是一个字符串。这可以在代码的其他地方使用来替换或删除钩子。

标识符应该是唯一的,这样您就不会意外覆盖其他一些 mods 钩子,除非您正在尝试这样做。

标识符可以是stringtable/对象,其中定义了IsValid 函数,例如EntityPanel。例如,numbersbooleans 是不允许的。

如果标识符是一个表/对象,它将被插入到回调中其他参数的前面,只要它有效,就会调用钩子。但是,只要 IsValid(identifier) 返回 false,挂钩就会被移除。

所以根据这个你需要改变你的第二个参数hook.Add (在你的情况下,你的两个钩子都是"chat")每个钩子都是唯一的,不会覆盖你的其他钩子。

【讨论】:

  • @Ryke 这对你有帮助吗?
【解决方案2】:

这种方式可行,因为这是我在我的服务器上所做的。

if(CLIENT) then
    concommand.Add("steam", function()
        gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")
    end)
end
if(SERVER) then
    hook.Add("PlayerSay", "OpenSteam", function(calling, txt, t)
        local args = string.lower(txt)
        if(args == "!steam") then
            calling:ConCommand("steam")
            return ""
        end
    end)
end

您可以在您的不和谐邀请中执行相同的操作。

编辑:

我从来没有意识到你已经打印出来聊天,所以我会告诉你一个很好的方法来做到这一点(或修复它):

if(CLIENT) then
    concommand.Add("steam", function()
        gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")
    end)
    concommand.Add("discord", function()
        gui.OpenURL("https://discord.gg/35rQxcE")
    end)
    concommand.Add(".prt", function(calling, cmds, args, argStr)
        chat.AddText(Color(255, 255, 255), "Player", Color(98, 176, 255), args[1], Color(255, 255,255), " has used ", Color(255, 62, 62), "!steam", Color(255, 255, 255), " to view our community Steam Group!")
    end)
    concommand.Add(".disprt", function(calling, cmds, args, argStr)
        chat.AddText(Color(255, 255, 255), "Player", Color(98, 176, 255), args[1], Color(255, 255,255), " has used ", Color(255, 62, 62), "!discord", Color(255, 255, 255), " to view our official Discord server!")
    end)
end
if(SERVER) then
    hook.Add("PlayerSay", "ChatCommands", function(calling, txt, t)
        local args = string.lower(txt)
        if(args == "!steam") then
            calling:ConCommand("steam")
            for _, ply in pairs(player.GetAll()) do
                ply:ConCommand(".prt " .. calling:Nick())
            end
            return ""
        end
        if(args == "!discord") then
            calling:ConCommand("discord")
            for _, ply in pairs(player.GetAll()) do
                ply:ConCommand(".disprt " .. calling:Nick())
            end
            return ""
        end
    end)
end

编辑 2:

另一个可能的错误原因是两个钩子具有相同的名称,"Chat"

【讨论】:

    【解决方案3】:

    问题是你覆盖了钩子。您应该将第二个参数更改为更具描述性的内容,例如:

    hook.Add( "PlayerSay", "ChatSteamGroup", steamgroupCommand )

    hook.Add( "PlayerSay", "ChatDiscord", discordCommand )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2013-04-04
      相关资源
      最近更新 更多