Distance / Radius Chat - Whispering not working

I found this code on the forums, but I noticed that most “whispers” or “/w” are not sending to players who are within the proximity/distance/radius mentioned in the script. The whisper does show for the user who sends the whisper, but the user receiving the whisper does not see a copy of it.

 // FileName: ProximityChat.lua
// Written by: TheGamer101
// Description: Only allows players to see messages from those within a certain distance.

local Chat = game:GetService("Chat")
local RunService = game:GetService("RunService")

local ReplicatedModules = Chat:WaitForChild("ClientChatModules")
local ChatSettings = require(ReplicatedModules:WaitForChild("ChatSettings"))

local PROXIMITY_DISTANCE = 50

local function Run(ChatService)

local function WithinProximity(player1, player2) 
	if player1 == player2 then
		return true
	end
	
	if player1.Character and player2.Character then
		local torso1 = player1.Character:FindFirstChild("HumanoidRootPart")
		local torso2 = player2.Character:FindFirstChild("HumanoidRootPart")
		if torso1 and torso2 then
			return (torso1.Position - torso2.Position).magnitude < PROXIMITY_DISTANCE
		end
	end
	return false
end

local function ProximityChatReplicationFunction(speakerName, message, channelName)
	local speakerObj = ChatService:GetSpeaker(speakerName)
	local channelObj = ChatService:GetChannel(channelName)
	
	if not speakerObj then return false end		
	if not channelObj then return false end		
	
	local playerObj = speakerObj:GetPlayer()
	
	if not playerObj then return false end
	
	for i, otherSpeakerName in pairs(channelObj:GetSpeakerList()) do
		local otherSpeakerObj = ChatService:GetSpeaker(otherSpeakerName)
		if otherSpeakerObj then
			local otherPlayerObj = otherSpeakerObj:GetPlayer()
			if otherPlayerObj then
				if WithinProximity(playerObj, otherPlayerObj) then
					otherSpeakerObj:SendMessage(message, channelName, speakerName, message.ExtraData)
				end
			end
		end
	end

	
	return true
end

ChatService:RegisterProcessCommandsFunction("proximity_chat", ProximityChatReplicationFunction, ChatSettings.LowPriority)
end

return Run

EDIT / UPDATE: The whispers are being delivered, but they are being delivered to the wrong user!

2 Likes

Looks fine at glance, try looking in output for errors and print to debug. See what values you have and what conditions are being met. When you say wrong user, is it delivered to “one” wrong user or is it inverted and sends it to everyone that is beyond the distance?

It’s a bit inconsistent. Most of the time it delivers to just one user (the wrong user), but sometimes it delivers to all users except the right user. Sometimes it just doesn’t deliver to anyone.

It definitely never sends to the correct user and it normally sends to the same player depending who you whisper.

For example, if there are two players, neither whisper gets delivered. But if there are three players, if player one whispers player two, it is player three who sees the message instead of player two…

Kind of sounds like you’ve inverted the distance check.

I mean, it works perfectly fine when you talk normally. The distance check and everything is fine, but as soon as you whisper it doesn’t do it correctly. Very strange.