I am trying to make a proximity chat, in which you get the message if the player sending it is within a certain distance from you. My friend found Roblox’s official example in the documentation, but when I paste it in, it doesn’t seem to work. When I try to local server test with two players, player 2 still receives a message from player 1 despite being 500 studs away, while I set it to not let anyone above 50.
I tried debugging it with breakpoints and print, but the ShouldDeliverCallback
never gets called. I even tried it with my friend through the player (not studio), and sure enough, I still get his message despite being 500 studs away, and nothing gets printed in the server log.
The script itself is inside ServerScriptService with Server RunContext.
local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral
local function getPositionFromUserId(userId: number)
local targetPlayer = Players:GetPlayerByUserId(userId)
if targetPlayer then
local targetCharacter = targetPlayer.Character
if targetCharacter then
return targetCharacter:GetPivot().Position
end
end
return Vector3.zero
end
generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
local targetPos = getPositionFromUserId(targetTextSource.UserId)
print("proximity debug " .. (targetPos - sourcePos).Magnitude)
return (targetPos - sourcePos).Magnitude < 50
end
the docs that I used: