Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Global chat proximity whispering doesn't work?

Asked by 4 years ago
--  // 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 = 100

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("UpperTorso")
            local torso2 = player2.Character:FindFirstChild("UpperTorso")
            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

Whispering doesn't seem to work. What can I do to fix? Thanks in advance!

Answer this question