Hello,
I am having issues with creating an unable to chat tag for players on the client.
First off, I have this function on the Server (PlayerManager), which should get all users that a player can chat with. I have it batches as Roblox seems to have a limit to how many players you can get at once, so I put it as a 25 player batch.
local function getUserIds()
local userIds = {}
for _, plr in srv.players:GetPlayers() do
table.insert(userIds, plr.UserId)
end
return userIds
end
local MAX_BATCH : number = 25
local function getPlayersCanChatWith(getUserId : number)
local userIds = getUserIds()
if Shared.IsStudio then
return userIds
end
local allowedUsers = {}
for i = 1, #userIds, MAX_BATCH do
local batch = {}
for j = 1, math.min(i + MAX_BATCH - 1, #userIds) do
table.insert(batch, userIds[j])
end
local succ, result = pcall(function()
return srv.text:CanUsersDirectChatAsync(getUserId, batch)
end)
if succ and result then
for _, userId in result do
if table.find(allowedUsers, userId) then
continue
end
table.insert(allowedUsers, userId)
end
end
task.wait(0.05)
end
return allowedUsers
end
Now, when the player joins, I call an event to that player to the Client, letting them know who they can chat with
for _, plr : Player in srv.players:GetPlayers() do
local char : Model = plr.Character
if not (plr ~= player and char) then
-- If no character, skip
continue
end
local head : BasePart = char:WaitForChild("Head", 5)
local tag : BillboardGui = head and head:FindFirstChild("OverheadGui")
local succ, err = pcall(function()
self.Client.CharacterAdded:Fire(player, plr, char, tag, getPlayersCanChatWith(plr.UserId))
end)
if Shared.IsStudio and not succ then
warn(err)
end
end
Then, also, when a new character is added, it sends an event to every player in the game letting them know if they can chat with that player or not
player.CharacterAdded:Connect(function(character : Model)
local tag : BillboardGui = client:AssignTag()
local succ, err = pcall(function()
self.Client.CharacterAdded:FireAll(player, character, tag, getPlayersCanChatWith(player.UserId))
end)
if Shared.IsStudio and not succ then
warn(err)
end
end)
Then, on the Client, I am setting the tag to visible if their UserID is found inside of the table that getPlayersCanChatWith gives
local function initChar(plr : Player, char : Model, tag : BillboardGui, canChat : NumberArray)
if not (plr ~= player and char and tag) then
return
end
canChat = canChat or {}
local icon : ImageLabel = tag:WaitForChild("Icons"):WaitForChild("NoChat", 5)
if not icon then
return
end
icon.Visible = not table.find(canChat, player.UserId)
end
PlayerManager.CharacterAdded:Connect(initChar)
So, that is the entire setup process. For some reason, almost every user is set as they cannot talk with eachother, even though they can. Proof below:
So, what can be causing this? Am I calling it too much? Should I cache things? Does anyone else have any experience in this that they can give me.
Thanks.
