근접 기반 채팅

*이 콘텐츠는 AI(베타)를 사용해 번역되었으며, 오류가 있을 수 있습니다. 이 페이지를 영어로 보려면 여기를 클릭하세요.

이 예제에서는 게임 세계에서 서로 가까운 사용자를 위한 독점 채팅을 구현하는 방법을 보여줍니다.TextSource를 사용하여 함수로 콜백을 확장하여 잠재적인 메시지 수신자의 위치를 식별하는 사용자를 식별합니다.이 함수가 false를 반환하면 사용자가 메시지 발신자의 유효 범위보다 멀리 위치한다는 것을 의미하므로 시스템이 해당 사용자에게 메시지를 전달하지 않습니다.

다음을 Class.Script``Class.ServerScriptService 추가하십시오:

서버

local TextChatService = game:GetService("TextChatService")
local Players = game:GetService("Players")
-- 근접 기반 채팅에 대한 채팅 채널 가져오기
-- 이 일반 채널을 전용 채널로 교체할 수 있습니다
local generalChannel: TextChannel = TextChatService:WaitForChild("TextChannels").RBXGeneral
-- 사용자 캐릭터의 위치를 가져오는 함수
local function getPositionFromUserId(userId: number)
-- 지정된 사용자 ID와 연결된 플레이어 가져오기
local targetPlayer = Players:GetPlayerByUserId(userId)
-- 플레이어가 존재하면 캐릭터의 위치를 가져옵니다
if targetPlayer then
local targetCharacter = targetPlayer.Character
if targetCharacter then
return targetCharacter:GetPivot().Position
end
end
-- 플레이어나 캐릭터를 찾을 수 없으면 기본 위치를 반환합니다
return Vector3.zero
end
-- 일반 채널에 대한 콜백을 설정하여 메시지 전달 제어
generalChannel.ShouldDeliverCallback = function(textChatMessage: TextChatMessage, targetTextSource: TextSource)
-- 메시지 발신자와 대상의 위치 가져오기
local sourcePos = getPositionFromUserId(textChatMessage.TextSource.UserId)
local targetPos = getPositionFromUserId(targetTextSource.UserId)
-- 발신자와 대상 사이의 거리가 50 단위 미만인 경우 메시지 전달
return (targetPos - sourcePos).Magnitude < 50
end