SocialService

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
サービス
複製されていません

SocialService は、Roblox プラットフォーム上で作成された関係に影響を与えるソーシャル機能を簡素化します。主な使用は、招待プロンプトと電話帳をプレイヤーに表示し、それらがそれぞれPromptGameInvite()PromptPhoneBook()を通じて友達に招待リクエストを送信できるようにすることです。そのようなリクエストが行われたときに、シグナルを利用できます。

概要

方法

イベント

コールバック

プロパティ

方法

GetPlayersByPartyId

Instances

現在接続されているすべての Player オブジェクトのテーブルを返し、その Player.PartyId プロパティが提供された partyId に一致します。このメソッドは、Players:GetPlayers() と同様に動作しますが、結果を指定されたパーティに属するプレイヤーのみを含むようにフィルタリングします。

このサービスは、Roblox Studio でのプレイテスト中には機能しません;経験の側面をテストするには、経験を公開し、Roblox アプリケーションでプレイする必要があります。

パラメータ

partyId: string
既定値: ""

戻り値

Instances

パスされた プロパティと一致するオブジェクトの テーブル。

コードサンプル

The following example listens for when a player joins the experience. If the player is part of a party, it uses SocialService:GetPlayersByPartyId() to retrieve all players in the same party who are currently connected to the server. It then outputs the name of each party member.

SocialService:GetPlayersByPartyId()

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local partyId = player.PartyId
if partyId ~= "" then
local partyPlayers: { Player } = SocialService:GetPlayersByPartyId(partyId)
for _, partyMember in partyPlayers do
print("Party Member: " .. partyMember.Name)
end
else
warn("Player is not in a party")
end
end)

This code sample demonstrates how to assign players to Teams based on their Player.PartyId using the SocialService:GetPlayersByPartyId() method. When a player joins the experience, the script checks whether they're part of a party. If so, it attempts to assign them to the same team as other party members. If no match is found or the player isn't in a party, they're placed on the team with fewer members to maintain balance.

Team Rebalance with Party Integration

local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local redTeam = Instance.new("Team")
redTeam.Name = "Red Team"
redTeam.TeamColor = BrickColor.Red()
redTeam.AutoAssignable = false
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.Name = "Blue Team"
blueTeam.TeamColor = BrickColor.Blue()
blueTeam.AutoAssignable = false
blueTeam.Parent = Teams
local function assignPlayerToTeam(player)
local partyId = player.PartyId
if partyId == "" then
assignBalancedTeam(player)
return
end
local teams = { redTeam, blueTeam }
local matchingTeam = nil
local partyPlayers = SocialService:GetPlayersByPartyId(partyId)
for _, partyPlayer in partyPlayers do
if partyPlayer ~= player and table.find(teams, partyPlayer.Team) then
matchingTeam = partyPlayer.Team
break
end
end
if matchingTeam then
player.Team = matchingTeam
player.TeamColor = matchingTeam.TeamColor
else
assignBalancedTeam(player)
end
end
function assignBalancedTeam(player)
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local chosenTeam = redCount <= blueCount and redTeam or blueTeam
player.Team = chosenTeam
player.TeamColor = chosenTeam.TeamColor
end
local function groupPlayersByParty()
local seenPartyIds = {}
local groupedPlayers = {}
for _, player in Players:GetPlayers() do
if player.PartyId == "" then
table.insert(groupedPlayers, { player })
elseif not table.find(seenPartyIds, player.PartyId) then
table.insert(seenPartyIds, player.PartyId)
local partyPlayers = SocialService:GetPlayersByPartyId(player.PartyId)
table.insert(groupedPlayers, partyPlayers)
end
end
return groupedPlayers
end
-- Call this function to rebalance teams
local function rebalanceTeams()
local groups = groupPlayersByParty()
table.sort(groups, function(a, b)
return #a > #b
end)
for _, player in Players:GetPlayers() do
player.Team = nil
end
for _, group in groups do
local redCount = #redTeam:GetPlayers()
local blueCount = #blueTeam:GetPlayers()
local bestTeam
if redCount <= blueCount then
bestTeam = redTeam
else
bestTeam = blueTeam
end
for _, player in group do
player.Team = bestTeam
player.TeamColor = bestTeam.TeamColor
end
end
for _, player in Players:GetPlayers() do
player:LoadCharacter()
end
end
Players.PlayerAdded:Connect(function(player)
assignPlayerToTeam(player)
player:LoadCharacter()
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
assignPlayerToTeam(player)
end
end)
end)

HideSelfView

()

呼び出されるプレイヤーの自己ビューを非表示にします。このメソッドが自己ビューがすでに非表示で呼ばれているときに呼ばれると、何もしません。


戻り値

()

PromptGameInvite

()

PromptGameInvite() は、現在のエクスペリエンスに友達を招待できるように、ローカルプレイヤーに招待プロンプトを表示します。このメソッドを呼び出す前に、CanSendGameInviteAsync() を使用して、プレイヤーが招待を送信できるかどうかを判断する必要があります。この能力は、プラットフォームやプレイヤーによって異なる可能性があるためです。

プレイヤーへの招待プロンプトの実装、招待プロンプトのカスタマイズ、およびランチデータの使用に関する詳細は、プレイヤーへの招待プロンプトを参照してください。

パラメータ

player: Instance

招待ポップアップで促す Player

既定値: ""
experienceInviteOptions: Instance

オプションの ExperienceInviteOptions オブジェクトで、プロンプトをカスタマイズ。

既定値: "nil"

戻り値

()

コードサンプル

The following code sample uses CanSendGameInviteAsync() to confirm whether the local Player can send an invite. If true, it then prompts the invite using PromptGameInvite().

Sending an Invite

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Function to check whether the player can send an invite
local function canSendGameInvite(sendingPlayer)
local success, canSend = pcall(function()
return SocialService:CanSendGameInviteAsync(sendingPlayer)
end)
return success and canSend
end
local canInvite = canSendGameInvite(player)
if canInvite then
SocialService:PromptGameInvite(player)
end

PromptPhoneBook

()

指定された Player に電話帳を使って確認するプレイヤーが誰かを呼ぶことを選択すると、CallInviteStateChanged イベントが発動します。電話帳を表示できる能力はプレイヤーによって異なるため、PromptPhoneBook() を呼び出す前に CanSendCallInviteAsync() を使用する必要があります。

プレイヤーが電話帳を開く資格がない場合、エラーダイアログが表示されます。

このメソッドのサンプル実装は、Roblox Connect を参照してください。

パラメータ

player: Instance

電話帳で質問するプレイヤー。

既定値: ""
tag: string

様々な電話帳の「エントリポイント」や同様のものを区別するのに役立つ文字列。たとえば、呼び出しプレイヤーのキャラクターが現在どのエクスペリエンス領域にいるかを定義する文字列をパスすることができます。

既定値: ""

戻り値

()

コードサンプル

The following code sample, placed within a child LocalScript of a GuiButton, uses CanSendCallInviteAsync() to confirm that the player can make a call. If so, it connects PromptPhoneBook() to the button's Activated event.

SocialService:PromptPhoneBook()

local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local player = Players.LocalPlayer
local button = script.Parent
button.Visible = false
-- Function to check whether the player can send a call invite
local function canSendCallingInvite(sendingPlayer)
local success, canSend = pcall(function()
return SocialService:CanSendCallInviteAsync(sendingPlayer)
end)
return success and canSend
end
local canCall = canSendCallingInvite(player)
if canCall then
button.Visible = true
button.Activated:Connect(function()
SocialService:PromptPhoneBook(player, "")
end)
end

ShowSelfView

()

呼び出されたプレイヤーの自己ビューを表示します。このメソッドが自己ビューがすでに表示されているときに呼び出された場合、何も行わないことがあります。

パラメータ

selfViewPosition: Enum.SelfViewPosition

自分のビューを配置する位置。

既定値: "LastPosition"

戻り値

()

CanSendCallInviteAsync

イールド

指定された Player が友達に呼び出しの招待を送信できる場合、true を返します。電話帳を開く能力はプレイヤーによって異なるため、PromptPhoneBook() を呼び出す前に常にこのメソッドの結果を使用する必要があります。

このメソッドのサンプル実装は、Roblox Connect を参照してください。

パラメータ

player: Instance

プレイヤーの Player インスタンスが電話の招待を送信する可能性があります。

既定値: ""

戻り値

指定されたプレイヤーが呼び出しを送信できるかどうか。

コードサンプル

The following code sample, placed within a child LocalScript of a GuiButton, uses CanSendCallInviteAsync() to confirm that the player can make a call. If so, it connects PromptPhoneBook() to the button's Activated event.

SocialService:PromptPhoneBook()

local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local player = Players.LocalPlayer
local button = script.Parent
button.Visible = false
-- Function to check whether the player can send a call invite
local function canSendCallingInvite(sendingPlayer)
local success, canSend = pcall(function()
return SocialService:CanSendCallInviteAsync(sendingPlayer)
end)
return success and canSend
end
local canCall = canSendCallingInvite(player)
if canCall then
button.Visible = true
button.Activated:Connect(function()
SocialService:PromptPhoneBook(player, "")
end)
end

CanSendGameInviteAsync

イールド

CanSendGameInviteAsync() は、指定された Player が現在のエクスペリエンスに他のプレイヤーを招待できる場合、true を返します。プレイヤーを招待できる能力は、プラットフォームやプレイヤーによって異なるため、PromptGameInvite() を呼び出す前に、このメソッドの結果を常に使用する必要があります。

プレイヤーの招待プロンプトの実装、プロンプトと通知のカスタマイズ、およびランチデータの使用に関する詳細は、プレイヤー招待プロンプト を参照してください。

パラメータ

player: Instance

プレイヤーのインスタンスが招待を送信する可能性がある Player

既定値: ""
recipientId: number

オプションの Player.UserId ポテンシャル受信者 **** 、送信者が特定の受信者を招待できるかどうかをチェックするために使用されます。

既定値: 0

戻り値

指定されたプレイヤーが招待を送信できるかどうか。

コードサンプル

The following code sample uses CanSendGameInviteAsync() to confirm whether the local Player can send an invite. If true, it then prompts the invite using PromptGameInvite().

Sending an Invite

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Function to check whether the player can send an invite
local function canSendGameInvite(sendingPlayer)
local success, canSend = pcall(function()
return SocialService:CanSendGameInviteAsync(sendingPlayer)
end)
return success and canSend
end
local canInvite = canSendGameInvite(player)
if canInvite then
SocialService:PromptGameInvite(player)
end

GetEventRsvpStatusAsync

イールド

指定されたイベントのために、ローカルプレイヤーの RSVP ステータスを返します。イベントは現在のエクスペリエンスにある必要があり、すでに開始されていてはなりません。イベントがすでに開始された場合、このメソッドはエラーを返します。

プレイヤーにイベントの RSVP ステータスを変更するよう促すために PromptRsvpToEventAsync() を使用できることに注意してください。

パラメータ

eventId: string

イベントのイベントIDは、プレイヤーに RSVP ステータスを変更するよう促すイベントです。これは、現在のエクスペリエンスに存在する有効なイベント IDであり、文字列 (数字ではない) として表現されなければなりません。

既定値: ""

戻り値

イベントのためのプレイヤーの現在の RSVP ステータスを示す Enum.RsvpStatus を返します。プレイヤーがイベントにRSVPしていない場合、これは Enum.RsvpStatus.None を返します。

コードサンプル

The following example checks if a player is RSVP'd to an event; if not, it gives them the option to RSVP. If the player is already RSVP'd, it gives them the option to cancel their RSVP.

The following Script assumes that RunContext is set to Enum.RunContext.Client and that two sibling ProximityPrompts are in place: one for following the event and one for unfollowing the event. The prompts are enabled or disabled based on the player's RSVP status.

SocialService:PromptRsvpToEventAsync()

local SocialService = game:GetService("SocialService")
local EVENT_ID = "YOUR_EVENT_ID"
local followPrompt : ProximityPrompt = script.Parent.FollowProximityPrompt
local unFollowPrompt : ProximityPrompt = script.Parent.UnfollowProximityPrompt
local function updatePrompt(rsvpStatus : Enum.RsvpStatus)
if rsvpStatus == Enum.RsvpStatus.Going then
unFollowPrompt.Enabled = true
followPrompt.Enabled = false
else
unFollowPrompt.Enabled = false
followPrompt.Enabled = true
end
end
local success, currentRsvpStatus = pcall(function()
return SocialService:GetEventRsvpStatusAsync(EVENT_ID)
end)
if not success then
-- Could not retrieve RSVP status; don't enable either proximity prompt
warn("Failed to get RSVP status:", currentRsvpStatus)
return
end
print("CurrentRsvpStatus:", currentRsvpStatus)
updatePrompt(currentRsvpStatus)
unFollowPrompt.Triggered:Connect(function(player)
local rsvpStatus = SocialService:PromptRsvpToEventAsync(EVENT_ID)
updatePrompt(rsvpStatus)
end)
followPrompt.Triggered:Connect(function(player)
local rsvpStatus = SocialService:PromptRsvpToEventAsync(EVENT_ID)
updatePrompt(rsvpStatus)
end)

GetPartyAsync

イールド

返すのは、指定された partyId に関連するすべてのメンバーに関するデータを含む辞書のアレイです。返された配列は、エクスペリエンス内のすべてのアクティブなサーバーインスタンスでパーティの現在の状態を反映し、各パーティメンバーがパーティへの招待を受け入れた時間で順序が付けられます。これは、配列の最初の要素が最も早く受け入れられ、最後の要素が最も最近のものであることを意味します。

このメソッドは、現在のエクスペリエンス内および異なるサーバー間のすべてのパーティメンバーに関する最新情報を取得するのに便利で、テレポート、マッチメイキング、またはパーティベースのゲームロジックなどの協調したグループ行動を可能にします。

返された配列の各辞書には、次のフィールドが含まれています:


<th>値タイプ</th>
<th>説明</th>
</tr>
</thead>
<tbody>
<tr>
<th><code>ユーザーID</code></th>
<td>番号</td>
<td>プレイヤーの <code>Class.Player.UserId</code> プロパティ。</td>
</tr>
<tr>
<th><code>プレースID</code></th>
<td>番号</td>
<td>パーティメンバーが現在いる場所の <code>Class.DataModel.PlaceId</code>。</td>
</tr>
<tr>
<th><code>ジョブID</code></th>
<td>文字列</td>
<td>現在ユーザーが居住しているサーバーインスタンスの <code>Class.DataModel.JobId</code></td>
</tr>
<tr>
<th><code>プライベートサーバーID</code></th>
<td>文字列</td>
<td>パーティメンバーがプライベートまたは予約サーバーにいる場合、<code>Class.DataModel.PrivateServerId</code> が適用されます。</td>
</tr>
<tr>
<th><code>予約サーバーアクセスコード</code></th>
<td>文字列</td>
<td>適用される場合は、現在ユーザーが居住している予約済みサーバーのアクセスコード。<code>Class.TeleportService:TeleportAsync()</code> を使用して、パーティメンバーを相互にテレポートするのに便利。</td>
</tr>
</tbody>
キー

このサービスは、Roblox Studio でのプレイテスト中には機能しません;経験の側面をテストするには、経験を公開し、Roblox アプリケーションでプレイする必要があります。

パラメータ

partyId: string
既定値: ""

戻り値

現在、エクスペリエンスにいる指定されたパーティのメンバーを表す辞書のアレイ。

コードサンプル

The following example checks if a player is in a party when they join. If so, it retrieves the latest party data using SocialService:GetPartyAsync() and outputs the details of each party member.

SocialService:GetPartyAsync()

local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local partyId = player.PartyId
if partyId == "" then
warn("Player is not in a party")
return
end
local success, partyData = pcall(function()
return SocialService:GetPartyAsync(partyId)
end)
if success and partyData then
for _, partyMemberData in partyData do
print(
partyMemberData.UserId,
partyMemberData.PlaceId,
partyMemberData.JobId,
partyMemberData.PrivateServerId,
partyMemberData.ReservedServerAccessCode
)
end
else
warn("Failed to retrieve party data")
end
end)

PromptRsvpToEventAsync

イールド

PromptRsvpToEventAsync() ローカルプレイヤーを通じて、指定のイベントに RSVP ステータスを変更するようなプロンプトを表示します。

イベントは現在のエクスペリエンスにある必要があり、すでに開始されていてはなりません。イベントがすでに開始されている場合、このメソッドはエラーを返します。

このメソッドを呼び出す前に、GetEventRsvpStatusAsync() を使用してプレイヤーの現在の RSVP ステータスをチェックできることに注意してください。

パラメータ

eventId: string

イベントのイベントIDは、プレイヤーに RSVP ステータスを変更するよう促すイベントです。これは、現在のエクスペリエンスに存在する有効なイベント IDであり、文字列 (数字ではない) として表現されなければなりません。

既定値: ""

戻り値

返す Enum.RsvpStatus プレイヤーの新しい RSVP ステータスを指示する後、プロンプが閉じられたことを示す。プレイヤーが RSVP ステータスを変更せずにプロンプトを閉じると、これは Enum.RsvpStatus.None または既にステータスを選択している場合は古い Enum.RsvpStatus を返します。

コードサンプル

The following example checks if a player is RSVP'd to an event; if not, it gives them the option to RSVP. If the player is already RSVP'd, it gives them the option to cancel their RSVP.

The following Script assumes that RunContext is set to Enum.RunContext.Client and that two sibling ProximityPrompts are in place: one for following the event and one for unfollowing the event. The prompts are enabled or disabled based on the player's RSVP status.

SocialService:PromptRsvpToEventAsync()

local SocialService = game:GetService("SocialService")
local EVENT_ID = "YOUR_EVENT_ID"
local followPrompt : ProximityPrompt = script.Parent.FollowProximityPrompt
local unFollowPrompt : ProximityPrompt = script.Parent.UnfollowProximityPrompt
local function updatePrompt(rsvpStatus : Enum.RsvpStatus)
if rsvpStatus == Enum.RsvpStatus.Going then
unFollowPrompt.Enabled = true
followPrompt.Enabled = false
else
unFollowPrompt.Enabled = false
followPrompt.Enabled = true
end
end
local success, currentRsvpStatus = pcall(function()
return SocialService:GetEventRsvpStatusAsync(EVENT_ID)
end)
if not success then
-- Could not retrieve RSVP status; don't enable either proximity prompt
warn("Failed to get RSVP status:", currentRsvpStatus)
return
end
print("CurrentRsvpStatus:", currentRsvpStatus)
updatePrompt(currentRsvpStatus)
unFollowPrompt.Triggered:Connect(function(player)
local rsvpStatus = SocialService:PromptRsvpToEventAsync(EVENT_ID)
updatePrompt(rsvpStatus)
end)
followPrompt.Triggered:Connect(function(player)
local rsvpStatus = SocialService:PromptRsvpToEventAsync(EVENT_ID)
updatePrompt(rsvpStatus)
end)

イベント

CallInviteStateChanged

このイベントは、プレイヤーの呼び出し状態が変更されたときに発動します。

パラメータ

player: Instance

呼び出しの招待状態変更があったプレイヤーの Player インスタンス

inviteState: Enum.InviteState

新しい呼び出し招待状態。


コードサンプル

SocialService.CallInviteStateChanged

local SocialService = game:GetService("SocialService")
local button = script.Parent
local isPhonebookOpen = false
SocialService.CallInviteStateChanged:Connect(function(_, inviteState)
local isCalling = inviteState == Enum.InviteState.Placed
if isCalling or isPhonebookOpen then
button.Visible = false
else
button.Visible = true
end
end)

GameInvitePromptClosed

このイベントは、プレイヤーが招待プロンプトを閉じると発動します。

パラメータ

player: Instance

プロンプトを閉じたプレイヤーの Player インスタンス。

recipientIds: Array

もう使用されていない; 空の配列。


PhoneBookPromptClosed

プレイヤーが電話帳のプロンプトを閉じると発火します。

パラメータ

player: Instance

電話帳を閉じたプレイヤーの Player インスタンス。


コールバック

OnCallInviteInvoked

電話帳からの呼び出しを処理するためのコールバック。tag パラメータは、PromptPhoneBook() で説明されているように、異なる「エントリポイント」や同様のものを区別するために使用できます。1つのコールバックのみを設定できます。

パラメータ

tag: string

様々な電話帳のエントリポイントを区別するのに役立つ文字列。

callParticipantIds: Array

呼び出しに関与するすべてのプレイヤーを含む配列。呼び出し者は常に配列の最初のプレイヤーになります。


戻り値

値が PlaceId および ReservedServerAccessCode のキーを含むテーブルは、それぞれ DataModel.PlaceId および TeleportService:ReserveServer() によって返されたサーバーアクセスコードです。

コードサンプル

SocialService.OnCallInviteInvoked

local SocialService = game:GetService("SocialService")
local TeleportService = game:GetService("TeleportService")
SocialService.OnCallInviteInvoked = function()
local placeId = 0123456789 -- This is the place ID of the desired place to drop call participants into
local accessCode = TeleportService:ReserveServer(placeId)
return { ReservedServerAccessCode = accessCode, PlaceId = placeId }
end