Help with New Proximity Chat (TextChatServices)

can someone explain to me the process of this script?

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
local generalChannel : TextChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral")

local function getPositionFromUserId(userId)
	local targetPlayer = Players:GetPlayerByUserId(userId)
	if targetPlayer then
		local targetCharacter = targetPlayer.Character or targetPlayer:WaitForChild("HumanoidRootPart")
		if targetCharacter then
			return targetCharacter:GetPivot().Position
		end
	end

	return Vector3.new(0, 0, 0)
end

generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
	local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
	local targetPos = getPositionFromUserId(targetTextSource.UserId)

	return (targetPos - sourcePos).magnitude < 50
end

I got this from the official post from the Staffs about the new Chat System and I tweaked it a bit by finding the HumanoidRootPart instead but apparently, it still is not detecting. I have the script in ServerScriptStorage as a ServerScript. I have custom Characters meaning I’m not using the regular blocky avatars.

I got the code example from this post

Anyone can help me out?

4 Likes

Probably its not working cause HumanoidRootPart is not a Model type. The method :GetPivot() works only for models not for parts.

Even if you are using custom rigs as characters, those still models, so you can use it without adding the line to search for a HumanoidRootPart or what kind of “Characters” are you giving to your players?

If you still want to change the code and use the HRP, just change a few lines, instead of:

local targetCharacter = targetPlayer.Character or targetPlayer:WaitForChild("HumanoidRootPart")
return targetCharacter:GetPivot().Position

just:

local targetCharacter = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
local HRP = targetCharacter:WaitForChild("HumanoidRootPart")
return HRP.Position

The script is simply getting the Character Model by using the Player’s ID gotten, in order to sending it back as a position of the model, so it can calculate if players are within 50 studs of distance in order to show the chat lines or not

1 Like

AWF YOU AGAIN :sob: you’ve helped me so much since the past days I’ve been posting here in Dev Forum, THOUGH I really so much appreciate your help. I’ll try this out rn :sparkles:

edit, currently this is how the code looks like rn:

		local targetCharacter = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
		local hrp = targetPlayer:WaitForChild("HumanoidRootPart")
		if hrp then
			return hrp.Position
		elseif not hrp then
			return targetPlayer:GetPivot().Position
		end
3 Likes

Sorry I did not follow a previous topic we were discussing about the tool’s tweening… :sweat_smile:

You sent enough data to analyze, but honestly I’m not having enough free time to carefuly check it, hope you found the fix

That wont work, targetPlayer holds a Player instance, not the character model, the HRP is inside the character model not the player instance, the player instance its like a… the “soul” of the player, not his body, the HRP its a part of his body not from his “soul”

local targetCharacter = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
local hrp = targetCharacter:WaitForChild("HumanoidRootPart") -- you want to look for the HRP inside the character model, not the player instance
if hrp then
	return hrp.Position
elseif not hrp then
	return targetCharacter:GetPivot().Position -- same you want to check the Pivot of the body not the "soul"
end
1 Like

so like should I set it to this instead?

		elseif not hrp then
			return targetPlayer.Character:GetPivot().Position
		end

and that’s fine !! I have it set aside for the moment since I don’t really need it for the mean time, I can work it on later after the main parts are finished in my game

also sorry for the late reply, my pc wen’t bonkers suddenly and I had to restart it forcefully :sob:

Not really, cause you already got the Character model in the targetCharacter variable, I just sent it like this:

local targetCharacter = targetPlayer.Character or targetPlayer.CharacterAdded:Wait()
local hrp = targetCharacter:WaitForChild("HumanoidRootPart")
if hrp then
	return hrp.Position
elseif not hrp and targetCharacter then
	return targetCharacter:GetPivot().Position
end

ohh alright, got you on that one, I’ll test this out really quick

apparently, it still doesn’t work. I’m far from my character, that is farther from the distance

I never tested the functions and callbacks of the new textservice, but look at the function at the bottom. Its reading a source and target:

local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
local targetPos = getPositionFromUserId(targetTextSource.UserId)

-- check these with the warns
warn(textChatMessage.TextSource.UserId) -- source player
warn(sourcePos) -- source pos
warn(targetTextSource.UserId) -- target player
warn(targetPos) -- target pos

Sounds like its calculating distance between specific source and target, not globally

alright hold on, Let me check it right now in studio.

@Dev_Peashie

apparently, its not returning a warn, I’ll try to switch with print?

Prints and warns are exactly the same, its just warns are prints on yellow color.
If this function doesnt show any prints then theres something else wrong

generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
warn("got callback")

	local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
	local targetPos = getPositionFromUserId(targetTextSource.UserId)

-- check these with the warns
warn(textChatMessage.TextSource.UserId) -- source player
warn(sourcePos) -- source pos
warn(targetTextSource.UserId) -- target player
warn(targetPos) -- target pos

	return (targetPos - sourcePos).magnitude < 50
end

well apparently, there is something wrong with the script.

I placed the ServerScript on ServerScriptService. I did not even get the warns


edit:
@Dev_Peashie I’m going to add warns/prints in every code to see what’s wrong

Did you research what setup is needed to fire that callback? like:

I could be pretty lost on the new textchatservice honestly I never used it, but if you follow whole documentation I bet its not really hard to use it

I’ll try and re-read again the document thanks :smiley: Imma make sure to try and check the script again in a bit

Im pretty busy right now, but I did check with this script in my experience, and it fires the callback correctly:

local TextChatService = game:GetService("TextChatService")
local generalChannel : TextChannel = TextChatService:WaitForChild("TextChannels"):WaitForChild("RBXGeneral")

generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
	warn("got callback")
	warn(textChatMessage.TextSource.UserId) -- source player
	warn(targetTextSource.UserId) -- target player
end

Disable your previous script and try this one, if this doesnt show the warns then there’s something else wrong with your setup. Literally I did nothing just adding this script in ServerScriptService

one question, this work even if I’m testing it by myself with 2 separate windows right? in studio like player 1 and player 2

what the hell it DID work when I tried on the other game, hold on… let me try this again

Yes it should work no matter if you are testing with 2 fake players on simulate mode. But… I think that you are checking the output on the “client” of your 2 players, this is a server script, it will print in the window of the server not on the clients