Mute Player Problems

So what I am trying to do is if an admin runs a command, it will jail the player but also mute them. And I cant seem to figure out what the problem actually is. Here is the main part of my script:

local ChatService = require(
game:GetService("ServerScriptService")
:WaitForChild("ChatServiceRunner")
:WaitForChild("ChatService")
)

local prefix = ":"
game.Players.PlayerAdded:connect(function(plr)
  plr.Chatted:connect(function(message)
    local msg = message:split(" ")
     if msg[1] == prefix.."jail" then
      local target = msg[2]
       ChatService:MuteSpeaker(target)
         -- rest of my code is here
       end
  end)
end)

I hope someone can help!

1 Like

Is there an error that has been output?

No, I don’t receive any error.

OK! A few things that were wrong with the code.

First of all the ChatService module takes too long to load and by then the player has already joined. To fix this ive done a little trick to mean it will load it after checking for the player to join.

Second, you need to get a channel, and then mute the player from that channel.
I’ve also made it a neat little function.

local prefix = ":"
local functions = {}
game.Players.PlayerAdded:connect(function(Player)
	Player.Chatted:connect(function(Message)
		functions.PlayerChatted(Player, Message)
	end)
end)


local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

function functions.PlayerChatted(Player, Message)
	local msg = Message:split(" ")
	if msg[1] == prefix.."jail" then
		local target = msg[2]
		print(target)
		local channel = ChatService:GetChannel("ALL")
		channel:MuteSpeaker(target)
	end
end

EDIT: If the answer works please mark it as the answer. Thanks.

2 Likes

Wow dude. This method worked like a charm. You deserve a cookie.

1 Like

I have another question. How did you get the chat service to load?

Because I’m getting another problem

-- script1
mainmod:Jail(plr)

--script2
local mainmodule = {}
local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

function mainmodule:Jail(plr)
	local channel = ChatService:GetChannel("ALL")
	channel:MuteSpeaker(plr)
	game:GetService("Players")[plr.Name].Warning1.Value = true
	workspace:WaitForChild(plr.Name).HumanoidRootPart.CFrame = CFrame.new(workspace.Jail.Teleporter.Position)
	workspace:WaitForChild(plr.Name).Humanoid.WalkSpeed = 0
	workspace:WaitForChild(plr.Name).Humanoid.JumpPower = 0
end

return mainmodule

EDIT: I’m not getting any errors either.

So what happens when you require chat service is, it waits for it to appear when you do the :waitChild, and then loads it.
The issue is :waitForChild is pausing your script waiting for the thing to load in.

So what I did was i made sure your function is run before you try to load in the chatservice module.

Do you know how I could do it in the module I showed you?

I’m going to need to see both that module and your script where you use that module

Actually, give me a moment. I may have figured it out.

DUDE YOUR METHOD IS GODLY! Tysm!!!

1 Like