I am trying to make it so that the chatbox can only show messages from people in a specific radius, for example, 100 studs. For the player, they wont see the chat of someone who is over 100 studs away. I am not talking about bubble chat, though. Just the normal chatbox on the top left.
I have tried searching for nearly an hour now on how I can do this, but haven’t been able to find a single thing related except for bubble chat. I would appreciate it if someone can help, or point me to a post that has already done this that I just haven’t found.
To do this, you can take advantage of the fact that TextChatService will ignore TextChatMessages with empty Text properties:
local LocalPlayer = game:GetService("Players").LocalPlayer
local LocalCam = workspace.CurrentCamera
local MAX_HEARING_DISTANCE = 50
function shouldIgnoreMessage(textMsg)
local messageSrc = textMsg.TextSource
local sourcePlayer = messageSrc and game:GetService("Players"):GetPlayerByUserId(messageSrc.UserId)
local sourcePlrChar = sourcePlayer and sourcePlayer.Character
if not sourcePlrChar then
return false
end
local localPosition = LocalPlayer.Character and LocalPlayer.Character:GetPivot().p or LocalCam.CFrame.p
local distance = (sourcePlrChar:GetPivot().p - localPosition).Magnitude
if distance > MAX_HEARING_DISTANCE then
return true
end
return false
end
game:GetService("TextChatService").OnIncomingMessage = function(textMsg: TextChatMessage)
if shouldIgnoreMessage(textMsg) then
textMsg.Text = ""
return
end
-- rest of your OnIncomingMessageLogic...
end
If the user who sent the message’s character is beyond MAX_HEARING_DISTANCE, the message will simply not be seen by them.