2017-09-06 92 views
0

因此,我目前正在嘗試創建一個小腳本,用於當GMOD中的玩家輸入「!content」時,例如,他們將通過將導致他們到Steam該服務器的內容。對於前面的例子,它的工作,所以我然後試圖複製的模板,只是改變功能名稱等我沒有遇到與示例相同的結果,但似乎沒有發生,我不知道爲什麼,因爲我只有改變了函數名和字符串。如果你能幫助我,這將是偉大的。提前致謝。Lua與GMOD自定義聊天腳本的問題

汽集團聊天腳本(工程)

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) 

齟齬聊天腳本(不工作)

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) 
+0

我忘了補充該文件位於\ garrysmod \ LUA \自動運行 – Ryke

+0

你有多個掛鉤?你似乎對兩者都使用相同的標識符;每個標識符只能有一個鉤子函數:https://wiki.garrysmod.com/page/hook/Add –

回答

0

如果你看看documentation of hook.Add,你會看到有關所需參數的細節。爲你

第二個參數是重要

  • any標識符
  • 的唯一標識符,通常是一個字符串。這可以在代碼的其他地方用來替換或刪除鉤子。

    標識符應該是唯一的這樣你就不會意外地覆蓋一些其他的mods鉤子,除非你正在嘗試這麼做。

    標識符可以是stringtable /定義了IsValid函數的對象,例如EntityPanel。例如,不允許使用numbersbooleans

    如果標識符是一個表/對象,它將被插入回調中其他參數的前面,並且只要該鉤子有效就會調用該鉤子。但是,只要IsValid(標識符)返回false,鉤子將被移除。

    所以根據這一點,你需要改變你的hook.Add (在你的情況是"chat"爲您的鉤子),第二個參數是唯一的每個鉤不會覆蓋你的其他掛鉤。

    +0

    @Ryke這對你有幫助嗎? – Mischa

    0

    這種方式將工作,因爲這是我在我的服務器上做的。

    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"

    0

    問題是您覆蓋掛鉤。你應該更改第二個參數爲更具說明性的,如:

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

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