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
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?
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
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)