luagarrys-mod

How to run a Function on a Player GMOD DarkRP Lua


I am trying to make a muting script that doesn't let players talk. At the moment I am at telling which player in the script with no commands but I cant work out or find how to run this function that I created on a specific player, not just mute the entire server.

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

I have so far got to using Player:checkmute(()targetPlayer) but I assume this is wrong


Solution

  • So at first a few mistakes I have seen on a first quick look.


    Things we will be using to achieve your desired task


    The solution

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

    Some helpful links


    Disclaimer

    This code is completely untested and written of my mind. If this does not work and throws some errors let me know that. I'll correct it then.