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