Hi, so I’m trying to disable the chat for a player for an in-game reason but I keep having a weird thing occuring. This is my code rn.
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
if player ~= nil then
local playerGui = player:FindFirstChild("PlayerGui")
if playerGui ~= nil then
print("Working")
print("1")
for i,v in pairs(playerGui:GetChildren()) do
print(v.Name)
if v.Name == "Chat" then
print("2")
v.Frame.ChatBarParentFrame.Frame.BoxFrame.Frame.ChatBar.Visible = false
v.Frame.ChatBarParentFrame.Frame.BoxFrame.Frame.TextLabel.Text = "You're out of Messages! {Early Access}"
l’m getting this as an output:
Why is it only registering these inside the PlayerGui while there is more items in it?
That part of code is located inside of a loop, When I tried doing that and running the script I got an error saying Attempt to index a local 'chat' (a nil value. and when I used a if chat ~= nil then it didn’t go through as the script somehow doesn’t grab all what is inside the PlayerGui. My script is inside the player’s character. When I tried that I used :FindFirstChild, I’ll try :WaitForChild rn.
For this, I would prefer to edit Roblox’s chat code directly
To get Roblox’s chat code, PlayTest your game and locate the Chat service. Copy all contents, Stop the PlayTest, and then paste them all into the now-empty Chat service you see in Studio.
Open the script ChatServiceRunner, and input this code at line 139, at the start of the SayMessageRequest listener
if playerObj:GetAttribute("Sienced") then
return
end
I believe this will stop their message from going through to other players, though their messages will still be seen by themselves. You could further restructure this by only allowing their message to go to a specific team of players.
Now, on the Server, when you want a player to be silenced, put this function somewhere and call it with the target player as the first parameter, and a boolean as the second parameter
function TogglePlayerSilence = function(player,bool)
player:SetAttribute("Silenced",bool)
end
After some digging with Ctrl+Shift+F, the phrase “To chat click here or press “/” key” appears in Chat.ChatScript.ChatMain at line 172
You could replace that portion of code with the following
if game.Players.LocalPlayer:GetAttribute("Silenced") then
if (UserInputService.TouchEnabled) then
ChatBar:SetTextLabelText(ChatLocalization:Get("GameChat_ChatMain_ChatBarTextTouch",'You are silenced'))
else
ChatBar:SetTextLabelText(ChatLocalization:Get("GameChat_ChatMain_ChatBarText",'You are silenced'))
end
else -- they are not silenced
if (UserInputService.TouchEnabled) then
ChatBar:SetTextLabelText(ChatLocalization:Get("GameChat_ChatMain_ChatBarTextTouch",'Tap here to chat'))
else
ChatBar:SetTextLabelText(ChatLocalization:Get("GameChat_ChatMain_ChatBarText",'To chat click here or press "/" key'))
end
end
This is not a graceful solution but it is a patch that you are unlikely to revisit.
Also, I wanted to mention a few things in your code from the OP
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
can be changed to
local player = game.Players.LocalPlayer
if player ~= nil then – you don’t need this at all, because if the local script is running, player will always exist, however, you can just say “if player then” since its existence counts as “true”
local playerGui = player:FindFirstChild(“PlayerGui”)
if playerGui ~= nil then
– if you want to make sure your PlayerGui is loaded before you continue any operation, you should say
player:WaitForChild(“PlayerGui”)
– now you won’t need to check if it exists or not
Oh and btw…
When I go to playerGui to get a copy of the chat so I can edit it and stuff it only shows what it has with no scripts…
How can I grab the scripts so I can edit them?
You might have to go into Studio’s Settings and check the box that says “Show Hidden Services” if you cannot find the “Chat” service. It looks like this for me
Are you trying to disable the Chat bar? Or not allow them to send messages?
You can require the ChatService module (game.Chat.ChatServiceRunner.ChatService) and then call the :GetChannel(“All”) method, to get the main chat channel. Then call :MuteSpeaker(speakerName, reason, duration) to mute them.
You could also fork ChatService and make a message filter, where if the player is supposed to be blocked from chatting at the time, it just won’t send their messages.
PS. reason and duration aren’t required for MuteSpeaker()
@MrLonely1221 understands the Roblox chat modules much better than I do, but (and forgive me for assuming) I think what he said is a bit above your level of expertise? Maybe ask him to elaborate on critical definitions like “require” and “call” and maybe show you a small examples of how to mute a player in the All channel from a server script.
I would rather you use his solution over mine, if you can. It’s far more graceful and takes advantage of the existing chat modules.