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

How would I go about making a comms table system?

Asked by
stupru 11
4 years ago
Edited 4 years ago

I'm working on a game, and it has communications tables. I have a proximity chat script (will put below), but I want it so that if you talk within ~10 studs of a comms table, someone who is within ~10 studs of a different comms table will be able to see your chat. I don't need a full script, I just have no clue where to even start.

Here's the proximity chat script, which is a ModuleScript, in game.Chat.ChatModules.ProximityChat:

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

Answer this question