学习
引擎类
SocialService
无法创建
服务
未复制

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处



API 参考
方法
CanSendCallInviteAsync
暂停
功能: Social
SocialService:CanSendCallInviteAsync(player:Instance):boolean
参数
player:Instance
返回
代码示例
SocialService:PromptPhoneBook()
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local player = Players.LocalPlayer
local button = script.Parent
button.Visible = false
-- 函数检查玩家是否可以发送呼叫邀请
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
暂停
功能: Social
SocialService:CanSendGameInviteAsync(
player:Instance, recipientId:User
参数
player:Instance
recipientId:User
默认值:"U1.AQAAAAAAAAAAAAAAAAAAAAA"
返回
代码示例
发送邀请
local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- 函数检查玩家是否可以发送邀请
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
暂停
功能: Social
SocialService:GetEventRsvpStatusAsync(eventId:string):Enum.RsvpStatus
参数
eventId:string
代码示例
社交服务:提示玩家 RSVP 事件异步()
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
-- 无法获取 RSVP 状态;不要启用任何接近提示
warn("获取 RSVP 状态失败:", currentRsvpStatus)
return
end
print("当前 RSVP 状态:", 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)

GetExperienceEventAsync
暂停
功能: Social
SocialService:GetExperienceEventAsync(eventId:string):ExperienceEvent?
参数
eventId:string
返回
ExperienceEvent?
代码示例
SocialService:GetUpcomingExperienceEventsAsync()
local SocialService = game:GetService("SocialService")
-- 发现下一个即将到来的/活动的事件并提示玩家回复确认
local function promptNextUpcomingEvent()
local success, eventsOrError = pcall(function()
return SocialService:GetUpcomingExperienceEventsAsync()
end)
if not success then
warn("加载即将到来的事件失败:", eventsOrError)
return
end
local upcomingEvents = eventsOrError
local selectedEvent
for _, event in ipairs(upcomingEvents) do
if not event.HasStarted and not event.HasEnded then
selectedEvent = event
break
end
end
if selectedEvent then
local success, result = pcall(function()
return SocialService:PromptRsvpToEventAsync(selectedEvent.Id)
end)
if not success then
warn("回复确认提示失败:", result)
end
else
warn("没有可供提示的未来事件。")
return
end
end
promptNextUpcomingEvent()

GetPartyAsync
暂停
功能: Social
SocialService:GetPartyAsync(partyId:string):{any}
参数
partyId:string
返回
代码示例
社交服务:获取派对异步数据()
local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local partyId = player.PartyId
if partyId == "" then
warn("玩家不在派对中")
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("获取派对数据失败")
end
end)

GetPlayersByPartyId
功能: Social
SocialService:GetPlayersByPartyId(partyId:string):{Player}
参数
partyId:string
返回
代码示例
社交服务:根据派对 ID 获取玩家()
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("派对成员: " .. partyMember.Name)
end
else
warn("玩家不在派对中")
end
end)
队伍重新平衡与派对集成
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local redTeam = Instance.new("Team")
redTeam.Name = "红队"
redTeam.TeamColor = BrickColor.Red()
redTeam.AutoAssignable = false
redTeam.Parent = Teams
local blueTeam = Instance.new("Team")
blueTeam.Name = "蓝队"
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
-- 调用这个函数来重新平衡团队
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:LoadCharacterAsync()
end
end
Players.PlayerAdded:Connect(function(player)
assignPlayerToTeam(player)
player:LoadCharacterAsync()
player:GetPropertyChangedSignal("PartyId"):Connect(function()
if player.PartyId ~= "" then
assignPlayerToTeam(player)
end
end)
end)

GetUpcomingExperienceEventsAsync
暂停
功能: Social
SocialService:GetUpcomingExperienceEventsAsync():{ExperienceEvent}
返回
{ExperienceEvent}
代码示例
SocialService:GetUpcomingExperienceEventsAsync()
local SocialService = game:GetService("SocialService")
-- 发现下一个即将到来的/活动的事件并提示玩家回复确认
local function promptNextUpcomingEvent()
local success, eventsOrError = pcall(function()
return SocialService:GetUpcomingExperienceEventsAsync()
end)
if not success then
warn("加载即将到来的事件失败:", eventsOrError)
return
end
local upcomingEvents = eventsOrError
local selectedEvent
for _, event in ipairs(upcomingEvents) do
if not event.HasStarted and not event.HasEnded then
selectedEvent = event
break
end
end
if selectedEvent then
local success, result = pcall(function()
return SocialService:PromptRsvpToEventAsync(selectedEvent.Id)
end)
if not success then
warn("回复确认提示失败:", result)
end
else
warn("没有可供提示的未来事件。")
return
end
end
promptNextUpcomingEvent()

HideSelfView
功能: Social
SocialService:HideSelfView():()
返回
()

PromptFeedbackSubmissionAsync
暂停
功能: Social
SocialService:PromptFeedbackSubmissionAsync(options:Dictionary?):()
参数
options:Dictionary?
返回
()
代码示例
社交服务:PromptFeedbackSubmissionAsync()
local SocialService = game:GetService("SocialService")
-- 提示反馈的函数
local function promptForFeedback()
local success, errorMessage = pcall(function()
SocialService:PromptFeedbackSubmissionAsync()
end)
if success then
print("反馈提示完成")
else
warn("提示反馈失败:", errorMessage)
end
end
-- 玩家达到里程碑时提示反馈
local function onMilestoneReached()
promptForFeedback()
end
-- 示例用法:连接到游戏事件
game.ReplicatedStorage.MilestoneReached.OnClientEvent:Connect(onMilestoneReached)
SocialService:PromptFeedbackSubmissionAsync() — 玩家支持
local SocialService = game:GetService("SocialService")
local function promptPlayerSupport()
local success, errorMessage = pcall(function()
SocialService:PromptFeedbackSubmissionAsync({
FeedbackType = Enum.FeedbackType.PlayerSupport,
})
end)
if success then
print("玩家支持提示完成")
else
warn("提示玩家支持失败:", errorMessage)
end
end
-- 示例用法:连接到 ScreenGui 中的帮助按钮
local helpButton = script.Parent:WaitForChild("HelpButton")
helpButton.Activated:Connect(function()
task.spawn(promptPlayerSupport)
end)

PromptGameInvite
功能: Social
SocialService:PromptGameInvite(
player:Instance, experienceInviteOptions:Instance
):()
参数
player:Instance
experienceInviteOptions:Instance
默认值:"nil"
返回
()
代码示例
发送邀请
local SocialService = game:GetService("SocialService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- 函数检查玩家是否可以发送邀请
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

PromptLinkSharing
已弃用

PromptLinkSharingAsync
暂停
功能: Social
SocialService:PromptLinkSharingAsync(
player:Player, options:Dictionary
参数
player:Player
options:Dictionary
默认值:"nil"
返回

PromptPhoneBook
功能: Social
SocialService:PromptPhoneBook(
player:Instance, tag:string
):()
参数
player:Instance
tag:string
返回
()
代码示例
SocialService:PromptPhoneBook()
local Players = game:GetService("Players")
local SocialService = game:GetService("SocialService")
local player = Players.LocalPlayer
local button = script.Parent
button.Visible = false
-- 函数检查玩家是否可以发送呼叫邀请
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

PromptRsvpToEventAsync
暂停
功能: Social
SocialService:PromptRsvpToEventAsync(eventId:string):Enum.RsvpStatus
参数
eventId:string
代码示例
社交服务:提示玩家 RSVP 事件异步()
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
-- 无法获取 RSVP 状态;不要启用任何接近提示
warn("获取 RSVP 状态失败:", currentRsvpStatus)
return
end
print("当前 RSVP 状态:", 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)

ShowSelfView
功能: Social
SocialService:ShowSelfView(selfViewPosition:Enum.SelfViewPosition):()
参数
selfViewPosition:Enum.SelfViewPosition
默认值:"LastPosition"
返回
()

活动
CallInviteStateChanged
功能: Social
SocialService.CallInviteStateChanged(
player:Instance, inviteState:Enum.InviteState
参数
player:Instance
inviteState:Enum.InviteState
代码示例
社交服务.通话邀请状态更改
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
功能: Social
SocialService.GameInvitePromptClosed(
player:Instance, recipientIds:{any}
参数
player:Instance
recipientIds:{any}

PhoneBookPromptClosed
功能: Social
SocialService.PhoneBookPromptClosed(player:Instance):RBXScriptSignal
参数
player:Instance

ShareSheetClosed
功能: Social
SocialService.ShareSheetClosed(player:Player):RBXScriptSignal
参数
player:Player

回调
OnCallInviteInvoked
功能: Social
SocialService.OnCallInviteInvoked(
tag:string, callParticipantIds:{any}
参数
tag:string
callParticipantIds:{any}
返回
代码示例
社交服务.呼叫邀请被调用
local SocialService = game:GetService("SocialService")
local TeleportService = game:GetService("TeleportService")
SocialService.OnCallInviteInvoked = function()
local placeId = 0123456789 -- 这是要将通话参与者丢弃到的目标地点的地点 ID
local accessCode = TeleportService:ReserveServerAsync(placeId)
return { ReservedServerAccessCode = accessCode, PlaceId = placeId }
end

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。