Lua问题与GMOD自定义聊天脚本

我目前正在试图为GMOD创建一个小脚本,当玩家输入“!content”等命令时,他们将被呈现一个窗口,该窗口将引导他们进入该服务器的Steam内容。对于上一个示例,它可以正常工作,所以我尝试复制模板并更改函数名称等内容。然而,我没有得到与上面的示例相同的结果,似乎什么也没有发生,而我又不知道为什么,因为我只更改了函数名称和字符串。如果你可以帮助我,那就太好了。先谢谢了。

Steam组聊天脚本(可以工作)

function steamgroupCommandplytext)
   如果 string.subtext,1,6)==“!steamthen
    plyPrintMessage(3,“它有效!”)
    plySendLua([[gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")]])
    for k,v在pairs(player.GetAll())do v:ChatPrint(“Player“..ply:Nick()..”使用‘!steam’查看我们的社区Steam小组!“)
    end
    end
end
hook.Add(“PlayerSay”,“Chat”,steamgroupCommand)

Discord聊天脚本(无法正常工作)

function discordCommandplytext)
    如果 string.subtext,1,8)==“!discordthen
    plyPrintMessage(3,“它有效!”)
    plySendLua([[gui.OpenURL("https://discord.gg/35rQxcE")]])
    for k,v在pairs(player.GetAll())do v:ChatPrint(“Player“..ply:Nick()..”使用‘!discord’查看我们的官方Discord服务器!“)
    end
    end
end
hook.Add(“PlayerSay”,“Chat”,discordCommand)
点赞
用户4273199
用户4273199

如果您查看[hook.Add的文档](https://wiki.garrysmod.com/page/hook/Add),您将看到有关所需参数的详细信息。

第二个参数对您很重要

  1. [任何] (https://wiki.garrysmod.com/page/Category:any)标识符

唯一标识符,通常为字符串。这可以在代码的其他位置中用于替换或删除钩子。

标识符应该是唯一的,这样您就不会意外覆盖其他模块的钩子,除非这正是您想要做的。

标识符可以是字符串,也可以是定义了IsValid函数的[表](https://wiki.garrysmod.com/page/Category:table)/对象,例如[Entity](https://wiki.garrysmod.com/page /Category:Entity)或[Panel](https://wiki.garrysmod.com/page/Category:Panel)。例如,数字和布尔值是不允许的。

如果标识符是表/对象,则将其插入回调中的其他参数之前,并且只要它有效就会调用钩子。但是,只要IsValid(identifier)返回false,钩子就会被删除。

因此,根据此,您需要更改 hook.Add的第二个参数(在您的情况下,对于您的两个钩子都是“chat”)使它们对于不覆盖其他钩子而言是唯一的。

2017-09-07 08:03:40
用户8812394
用户8812394

这种方法可以起作用,因为这是我在我的服务器上所做的。

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

您可以在您的Discord邀请中执行相同的操作。

编辑:

我从未意识到您将其打印到聊天中,因此我将向您展示一种好的方法来完成它(或修复它):

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”。

2017-10-22 04:51:54
用户9117496
用户9117496

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

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

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

2017-12-26 15:12:23