Adding Chat Channels accessible for players of a certain rank in a group

I’ve been making a game and I want to make chat channels available to only players of a certain rank in a group.

I acknowledge that Team Chat could be used, but am wondering if there is any alternative way that does not require the creation of teams.

4 Likes

This is possible via using the Lua Chat System’s ChatService and other APIs in it.

Firstly, you’ll want to put a Script in ServerScriptService which will handle all of this.
Secondly, require the ChatService - you’ll need to use WaitForChild due to how long it takes to be added.
Yet also define the neccessary variables:

local Players = game:GetService("Players") --// Needed for later
local ServerScriptService = game:GetService("ServerScriptService")
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))
local GroupId = 0 --// Put your's here
local CertainRank = "Mafia Boss" --// Put rank name here
local ChannelName = "SecretChat"

Thirdly, create the Channel and make joinable false (so people who can’t access it cannot join) yet also auto-join false:

local Channel = ChatService:AddChannel(ChannelName)
Channel.Joinable = false
Channel.AutoJoin = false
--// There's more customisation too

Forthly, Connect on SpeakerAdded which has the parameter of the name of the new player and get that player - then you’ll want to check if they have that rank, if so allow them to join:

ChatService.SpeakerAdded:Connect(function(PlayerName)
    local Player = Players:FindFirstChild(PlayerName)
    if Player and Player:GetRoleInGroup(GroupId) == CertainRank then
        local Speaker = ChatService:GetSpeaker(PlayerName)
        Speaker:JoinChannel(ChannelName)
    end
end)

Let me know how it goes!

33 Likes

It works, thank you so much.

As a related question, is it possible to rearrange the order in which the channels appear on the Channels Bar?

1 Like

The ChatChannelsBar is sadly undocumented, there might possibly be one so I’d recommend to search through the ClientChatModules.

That’s a shame, but thank you anyway. : )

Hi, I tried to use this script for my game, and I added multiple ranks and then used ‘or’ statements but then someone who wasnt one of those ranks got access to the chat. How can I fix that?

Mind if you show your code? It may be an issue with your logical operators.


P.S: For multiple ranks, it may be better to put the ids within an array and create a function to check whether or not they’re in one of the roles.

For example:

--!strict
local group_id = 1;
local ranks = {"Gamer", "Nerd");

local function allowed_in(player: Player)
    for _, rank in ipairs(ranks) do
        if (player:GetRoleInGroup(group_id) == rank) then
            return (true);
        end
    end

    --// It'll return nil (a falsey value), no need to return false
end
1 Like