社交服務 可以幫助在 Roblox 平台上建立的關係影響社交功能。其主要用途是向玩家顯示邀請提示和電話簿,讓他們通過PromptGameInvite()和PromptPhoneBook()分別向朋友發送邀請請求。當這些請求被提出時,您可以利用信號。
概要
方法
活動
當玩家的呼叫狀態變更時,發出火焰。
當玩家關閉邀請提示時發生火災。
當玩家關閉電話簿提示時發生火災。
回調
當從電話簿中呼叫時,呼叫回應。
屬性
方法
HideSelfView
隱藏呼叫玩家的自我檢視。如果這個方法在自我視圖已隱藏時被呼叫,它不會做任何事情。
返回
PromptGameInvite
PromptGameInvite() 顯示向本地玩家發出邀請提示,通過該提示他們可以邀請朋友參加目前的體驗。在呼叫此方法之前,您應該使用 CanSendGameInviteAsync() 來確定玩家是否可以發送邀請,因為此能力可能會根據平台或玩家而異。
請參閱玩家邀請提示了解更多關於實現邀請提示、自定義提示和通知以及使用發射數據的詳情。
參數
可選擇 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發生。您應該在呼叫 PromptPhoneBook() 之前使用 CanSendCallInviteAsync() ,因為能夠查看電話簿的能力可能會因玩家而異。
如果玩家無法打開電話簿,錯誤對話框會顯示。
請參閱 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
如果指定的 Player 可以向朋好友發送呼叫邀請,返回 true 。你應該總是在呼叫 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 可以邀請其他玩家進入當前體驗。你應該總是在呼叫 PromptGameInvite() 之前使用此方法的結果,因為邀請玩家的能力可能會因平台或玩家而異。
請參閱玩家邀請提示了解更多關於實現玩家邀請提示、自定義提示和通知以及使用發射數據的詳情。
參數
可選擇的 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 參數可用於區分不同的「入口點」或類似物,如在 PromptPhoneBook() 中所述。只能設設定一個回呼。
參數
返回
包括 PlaceId 和 ReservedServerAccessCode 鍵值為 DataModel.PlaceId 和由 TeleportService:ReserveServer() 返回的服務器存取代碼的表,分別為值。
範例程式碼
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