So want I want to do is this:
The farther the client is from the other players, the more transparent the message appears in the message box.
Example:
Player 1 is 200 studs away from Player 2.
Player 1 sees their message in the message box, however Player 2 can barely see it since it is transparent. This would apply to all players.
Player 3 cannot see Player 1’s message because they are 400 studs away.
I have this script below, but it doesn’t seem to submit any errors.
local ChatService = game:GetService("Chat")
local function CalculateTransparency(distance)
local maxDistance = 100
local transparency = 1 - math.min(distance / maxDistance, 1)
return transparency
end
ChatService.Chatted:Connect(function(message)
local senderPos = message.Sender.Character.Head.Position
local playerPos = game.Players.LocalPlayer.Character.Head.Position
local distance = (senderPos - playerPos).Magnitude
local transparency = CalculateTransparency(distance)
message.TextLabel.TextTransparency = transparency
end)
The reason why your code isn’t working is because the “message” argument you are trying to get isn’t the message bubble itself but rather the origin of the message (the part where the bubble comes from).
To get the result you are trying to look for you must copy the BubbleChat script from your PlayerScripts in game and modify it from there. I will see what I can do in the meantime!
Hey there! So you’re having trouble with your proximity chat script not working as intended, right?
The script you have is a good start, but there are a few issues with it. Firstly, CalculateTransparency function calculates the transparency correctly, but it’s always setting the maximum transparency distance to 100. It’s important to note that this value should be adjusted based on the actual size of the game world.
Also, the code connects to the Chatted event of the ChatService, which is fired when a player sends a chat message. But the code sets the TextTransparency property of the TextLabel object to the calculated transparency, which is not correct. Instead, you should set the transparency of the message GUI element that you want to display in the game.
Check this new version of your code
local ChatService = game:GetService("Chat")
local function CalculateTransparency(distance)
local maxDistance = 200 -- adjust this value based on the actual size of the game world
local transparency = 1 - math.min(distance / maxDistance, 1)
return transparency
end
ChatService.Chatted:Connect(function(message)
local senderPos = message.Sender.Character.Head.Position
local playerPos = game.Players.LocalPlayer.Character.Head.Position
local distance = (senderPos - playerPos).Magnitude
local transparency = CalculateTransparency(distance)
-- Add the code to set the transparency of the message GUI element here
-- Example:
-- message.GUIObject.TextTransparency = transparency
end)