SocialService 可以簡化與 Roblox 平台上的關係所造成的社交功能。它的主要用途是顯示 邀請提示 和電話書給玩家,讓他們能夠通過 Class.SocialService:PromptGameInvite()|PromptGameInv
概要
方法
活動
玩家的呼叫狀態變更時發射。
玩家關閉邀請提示時發射。
玩家關閉電話簿提示時發射。
回調
從電話資料庫呼叫時的回來。
屬性
方法
HideSelfView
隱藏玩家自觀的自觀檢視。如果此方法在自觀視圖已隱藏時呼叫,它沒有任何作用。
返回
PromptGameInvite
PromptGameInvite() 顯示邀請提示給本地玩家,讓他們邀請他們的朋友來當前體驗。在呼叫此方法之前,您應該使用 CanSendGameInviteAsync() 來確認玩家是否能夠發送邀請

有關邀請提示的實現、自訂提示和通知,以及使用啟動資料,請參閱玩家邀請提示。
參數
Class.Player 以提示彈出(視窗、通知)請 popup。
可選擇 ExperienceInviteOptions 對話框。
返回
範例程式碼
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().
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 事件會發動。您應該在使用 Class.SocialService:CanSendCallInv
如果玩家無資格打開電話簿,將顯示錯誤對話框。
有關此方法的示例實現,請參閱 Roblox Connect。
參數
返回
範例程式碼
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.
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
顯示玩家自觀的自觀檢視。如果此方法在自觀視圖已可見時呼叫,它就不會做任何事。
參數
放置自觀的位置。
返回
CanSendCallInviteAsync
如果 true 可以發送給朋好友的呼叫邀請,你就會收到 Player 的結果。你應該總是在呼叫 PromptPhoneBook() 之前使用此方法的結果,因為能夠打開手機書的能力可能會因玩家而變得不同。
有關此方法的示例實現,請參閱 Roblox Connect。
參數
返回
是否允許指定的玩家發送呼叫邀請。
範例程式碼
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.
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() 返回 true 如果指定的 Player 可以邀请其他玩家到当前体體驗。您应该总是使用此方法的结果,前提是您调用的 0> Class.SocialService:Prom
有關玩家邀請提示的更多內容,請參閱Player Invite Prompts。
參數
可選 Player.UserId 的潛在 接收人 ,用於檢查發件方可以邀請該特定接收收件人。
返回
是否可以發送邀請給指定的玩家。
範例程式碼
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().
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
活動
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
這個事件會在玩家關閉邀請提示時發生。
參數
回調
OnCallInviteInvoked
從電話資料庫放置呼叫時執行的回撥。 tag 參數可用於與不同的「入口點」或類似的區分,如在 Class.SocialService:PromptPhoneBook()|PromptPhoneBook() 中所述。只有一個回撥才能設設定。
參數
返回
包含 PlaceId 和 ReservedServerAccessCode 鑰匙的桌子,其值是 DataModel.PlaceId 和 2>Class.TeleportService:ReserveServer() 返回的伺服器存取代碼,分別是 5>Class.DataModel.PlaceId5> 和 8>Class.TeleportService:ReserveServer()8> 。
範例程式碼
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