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