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.
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
AWF YOU AGAIN 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
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
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
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
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
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
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
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
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