How do i get player chat messages in a certain radius?

  1. What do you want to achieve? Keep it simple and clear!
    I want to get all chat messages from players in a certain radius from another player, locally
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do it sob sob
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    None, and i haven’t found any topics on this

Help would be really useful!

2 Likes

You could store all chat messages in a server script like this

local messages = {
{
Text = "Hello",
Owner = 1234,
},
{
Text = "Yes",
Owner = 843,
}
}

And you could create a remote function that the client can invoke to get messages around a certain position, then the server would check all of the messages, check their owners position, and return them if they are close enough

1 Like

I was thinking of somehow using plr.Chatted paired with calculating the magnitude, but i wanna do this locally, would a for loop work for something like that?

1 Like

I don’t think that player.Chatted works locally, you can use that event and fire a remote event from the server to the client whenever someone chats, and after that you can display the chat message locally

1 Like

Make localscript in StarterPlayerScripts and put this script

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Chat = game:GetService("Chat")

local RADIUS = 50 -- Set the radius you want to check

local function onPlayerChatted(player, message)
    if player ~= LocalPlayer then
        local distance = (LocalPlayer.Character.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
        
        if distance <= RADIUS then
            print(player.Name .. ": " .. message) -- Replace this with your desired action
        end
    end
end

local function onPlayerAdded(player)
    player.Chatted:Connect(function(message)
        onPlayerChatted(player, message)
    end)
end

-- Connect existing players
for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end

-- Listen for new players joining
Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

Thanks so much!! This helped a lot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.