如何 运行 播放器上的功能 GMOD DarkRP Lua

How to run a Function on a Player GMOD DarkRP Lua

我正在尝试制作一个不让玩家说话的静音脚本。目前我正在告诉脚本中的哪个播放器没有命令,但我无法解决或找到如何 运行 我在特定播放器上创建的这个功能,而不仅仅是使整个服务器静音。

sv_mute.lua:

util.AddNetworkString( "mute_message" )

ismuted == false
targetPlayer == "Xx_Player_xX"

function checkmute()

    function SendMessage( ply, txt, pub )
        net.Start( "mute_message" )
            net.WriteString( "YOU ARE MUTED, SHUT UP" )
        net.Send( ply )
        return ""
    end
    hook.Add( "PlayerSay", "SendMessage", SendMessage, )
end

Player:checkmute(()targetPlayer)

cl_mute.lua:

function ReceiveMessage()
    local txt = net.ReadString()
    chat.AddText( Color( 0, 255, 0), txt)
end

到目前为止我必须使用 Player:checkmute(()targetPlayer) 但我认为这是错误的

因此,起初我在第一眼看到时就发现了一些错误。

  • 您定义了一个没有参数的函数,但您尝试用一个参数调用它
  • 您没有使用 Garry 给您的工具向玩家发送消息

我们将用来完成您想要的任务的东西


解决方案

-- sv_mute.lua

-- a list of muted players, you need to get them from somewhere (not part of this answer)
mutedPlayers = {
  "StupidMan" = true,
  "AnnoyingKid" = true,
  "ExGirlfriend" = true
}

-- the function that will check if a user is muted
local function checkMuted(--[[ string ]] playername)
  return mutedPlayers[playername]
  -- you probably need to change the code to something else
  -- based on how you store your muted players
end

hook.Add("PlayerSay", "VosemMute_PlayerSay", function(sender, message, teamChat)
  if ( checkMuted(sender:GetName()) ) then
    sender.PrintMessage(HUD_PRINTTALK, "You are muted!")
    return "" -- this will suppress the message
  end
end)

一些有用的链接


免责声明

此代码完全未经测试,是我自己写的。如果这不起作用并引发一些错误,请告诉我。那我再更正一下。