SocialService

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

Tidak Dapat Dibuat
Layanan
Tidak Direplikasi

Layanan Sosial memudahkan fungsi sosial yang mempengaruhi hubungan yang dibuat di platform Roblox.Penggunaan utamanya adalah untuk menampilkan undangan prompt dan buku telepon kepada pemain, memungkinkan mereka mengirim permintaan undangan ke teman mereka melalui PromptGameInvite() dan PromptPhoneBook() masing-masing.Anda dapat memanfaatkan sinyal saat permintaan seperti itu dibuat.

Rangkuman

Metode

Acara

Callback

Properti

Metode

GetPlayersByPartyId

Instances

Kembalikan tabel semua objek yang saat ini terhubung Player yang memiliki properti Player.PartyId yang cocok dengan partyId yang disediakan.Metode ini berperilaku serupa dengan Players:GetPlayers() tetapi menyaring hasil untuk hanya termasuk pemain yang berada di party yang ditentukan.

Perhatikan bahwa layanan ini tidak berfungsi selama pengujian permainan di Roblox Studio; untuk menguji aspek pengalaman Anda menggunakannya, Anda harus mempublikasikan pengalaman dan memainkannya di aplikasi Roblox.

Parameter

partyId: string
Nilai Default: ""

Memberikan nilai

Instances

Meja dari Player objek yang mana properti Player.PartyId sesuai dengan partyId yang dikirimkan.

Contoh Kode

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

()

Sembunyikan pandangan diri pemain yang dipanggil. Jika metode ini dipanggil saat pandangan diri sudah tersembunyi, tidak ada yang dilakukan.


Memberikan nilai

()

PromptGameInvite

()

PromptGameInvite() menampilkan prompt undangan ke pemain lokal melalui mana mereka dapat mengundang teman-teman mereka ke pengalaman saat ini.Sebelum memanggil metode ini, Anda harus menggunakan CanSendGameInviteAsync() untuk menentukan apakah pemain dapat mengirim undangan, karena kemampuan ini dapat bervariasi tergantung pada platform atau pemain.

Lihat Undangan Pemain Prompt untuk lebih banyak rincian tentang implementasi undangan prompt, kustomisasi prompt dan notifikasi, dan menggunakan data peluncuran.

Parameter

player: Instance

The Player untuk meminta dengan popup undangan.

Nilai Default: ""
experienceInviteOptions: Instance

Objek opsional ExperienceInviteOptions untuk menyesuaikan prompt.

Nilai Default: "nil"

Memberikan nilai

()

Contoh Kode

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

()

Meminta Player yang diberikan dengan buku telepon.Jika pemain memilih untuk memanggil seseorang, acara CallInviteStateChanged terjadi.Anda harus menggunakan CanSendCallInviteAsync() sebelum memanggil PromptPhoneBook() karena kemampuan untuk melihat buku telepon mungkin bervariasi tergantung pada pemain.

Jika pemain tidak memenuhi syarat untuk membuka buku telepon, dialog kesalahan ditampilkan.

Lihat Roblox Connect untuk implementasi sampel dari metode ini.

Parameter

player: Instance

Pemain untuk meminta dengan buku telepon.

Nilai Default: ""
tag: string

String untuk membantu membedakan antara berbagai "poin entri" buku telepon atau serupa.Sebagai contoh, Anda dapat melewati string yang mendefinisikan bagian mana dari pengalaman yang karakter pemanggil saat ini berada.

Nilai Default: ""

Memberikan nilai

()

Contoh Kode

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

()

Menunjukkan pandangan diri pemain yang dipanggil. Jika metode ini dipanggil saat pandangan diri sudah terlihat, tidak ada yang dilakukan.

Parameter

selfViewPosition: Enum.SelfViewPosition

Posisi untuk menempatkan pandangan diri sendiri.

Nilai Default: "LastPosition"

Memberikan nilai

()

CanSendCallInviteAsync

Hasil

Kembalikan true jika yang diberikan Player dapat mengirim undangan panggilan ke teman.Anda harus selalu menggunakan hasil metode ini sebelum memanggil PromptPhoneBook() karena kemampuan untuk membuka buku telepon mungkin bervariasi tergantung pada pemain.

Lihat Roblox Connect untuk implementasi sampel dari metode ini.

Parameter

player: Instance

Instansi Player pemain yang berpotensi mengirim undangan panggilan.

Nilai Default: ""

Memberikan nilai

Apakah pemain yang ditentukan dapat mengirim undangan panggilan.

Contoh Kode

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

Hasil

CanSendGameInviteAsync() kembali true jika yang diberikan Player dapat mengundang pemain lain ke pengalaman saat ini.Anda harus selalu menggunakan hasil metode ini sebelum memanggil PromptGameInvite() karena kemampuan untuk mengundang pemain dapat bervariasi tergantung pada platform atau pemain.

Lihat Undangan Pemain untuk lebih banyak rincian tentang implementasi undangan pemain, kustomisasi undangan dan pemberitahuan, dan menggunakan data peluncuran.

Parameter

player: Instance

Instansi Player pemain yang berpotensi mengirim undangan.

Nilai Default: ""
recipientId: number

Opsi dari penerima potensial , digunakan untuk memeriksa apakah pengirim dapat mengundang penerima spesifik itu.

Nilai Default: 0

Memberikan nilai

Apakah pemain yang ditentukan dapat mengirim undangan.

Contoh Kode

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

Hasil

Kembalikan status RSVP pemain lokal untuk acara yang diberikan.Acara harus berada di pengalaman saat ini dan tidak boleh sudah dimulai.Jika acara sudah dimulai, metode ini akan mengembalikan kesalahan.

Perhatikan bahwa Anda dapat menggunakan PromptRsvpToEventAsync() untuk meminta pemain untuk mengubah status RSVP mereka untuk acara tersebut.

Parameter

eventId: string

ID acara dari acara untuk meminta pemain mengubah status RSVP mereka.Ini harus menjadi ID acara yang valid yang ada di pengalaman saat ini, diwakili sebagai string (bukan angka).

Nilai Default: ""

Memberikan nilai

Kembalikan Enum.RsvpStatus menunjukkan status RSVP saat ini pemain untuk acara.Jika pemain belum menanggapi acara, ini akan kembali Enum.RsvpStatus.None .

Contoh Kode

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

Hasil

Kembalikan array kamus yang berisi data untuk semua anggota yang terkait dengan partyId yang diberikan.배열 yang dikembalikan mencerminkan status saat ini dari pihak dalam semua instansi server aktif dalam pengalaman dan diurutkan berdasarkan waktu anggota pihak menerima undangan pihak.Ini berarti elemen pertama dalam array adalah yang paling awal untuk diterima dan terakhir adalah yang paling baru.

Metode ini berguna untuk mengambil informasi terkini tentang semua anggota party saat ini dalam pengalaman dan di berbagai server, memungkinkan perilaku kelompok terkoordinasi seperti teleportasi, matchmaking, atau logika permainan berbasis kelompok.

Setiap kamus dalam array yang dikembalikan berisi bidang berikut:


<th>Jenis Nilai</th>
<th>Deskripsi</th>
</tr>
</thead>
<tbody>
<tr>
<th><code>ID Pengguna</code></th>
<td>angka</td>
<td>Properti <code>Kelas.Player.UserId</code> pemain.</td>
</tr>
<tr>
<th><code>TempatId</code></th>
<td>angka</td>
<td>Kelas <code>Class.DataModel.PlaceId</code> dari tempat anggota party saat ini berada.</td>
</tr>
<tr>
<th><code>ID Pekerjaan</code></th>
<td>teks</td>
<td>Kelas <code>Class.DataModel.JobId</code> dari instansi server di mana pengguna saat ini berada.</td>
</tr>
<tr>
<th><code>ID Server Pribadi</code></th>
<td>teks</td>
<td>Jika berlaku, <code>Class.DataModel.PrivateServerId</code> ketika anggota party berada di server pribadi atau cadangan.</td>
</tr>
<tr>
<th><code>Kode Akses Server Tersimpan</code></th>
<td>teks</td>
<td>Jika berlaku, kode akses untuk server reservasi di mana pengguna saat ini berada.Berguna untuk teleportasi anggota party ke satu sama lain menggunakan <code>Class.TeleportService:TeleportAsync()</code> .</td>
</tr>
</tbody>
Kunci

Perhatikan bahwa layanan ini tidak berfungsi selama pengujian permainan di Roblox Studio; untuk menguji aspek pengalaman Anda menggunakannya, Anda harus mempublikasikan pengalaman dan memainkannya di aplikasi Roblox.

Parameter

partyId: string
Nilai Default: ""

Memberikan nilai

Sebuah array kamus yang mewakili anggota party yang ditentukan yang saat ini berada dalam pengalaman.

Contoh Kode

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

Hasil

PromptRsvpToEventAsync() menampilkan prompt ke pemain lokal melalui mana mereka dapat mengubah status RSVP mereka ke acara yang diberikan.

Peristiwa harus berada di pengalaman saat ini dan tidak boleh sudah dimulai. Jika peristiwa sudah dimulai, metode ini akan mengembalikan kesalahan.

Perhatikan bahwa Anda dapat menggunakan GetEventRsvpStatusAsync() untuk memeriksa status RSVP saat ini pemain sebelum memanggil metode ini.

Parameter

eventId: string

ID acara dari acara untuk meminta pemain mengubah status RSVP mereka.Ini harus menjadi ID acara yang valid yang ada di pengalaman saat ini, diwakili sebagai string (bukan angka).

Nilai Default: ""

Memberikan nilai

Kembalikan Enum.RsvpStatus menunjukkan status RSVP baru pemain setelah prompt ditutup.Jika pemain menutup prompt tanpa mengubah status RSVP mereka, ini akan kembali Enum.RsvpStatus.None atau lama Enum.RsvpStatus jika mereka sudah memilih status.

Contoh Kode

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)

Acara

CallInviteStateChanged

Acara ini terjadi ketika status undangan panggilan pemain berubah.

Parameter

player: Instance

Instansi Player pemain yang memiliki perubahan status undangan panggilan.

inviteState: Enum.InviteState

Status undangan panggilan baru.


Contoh Kode

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

Acara ini terjadi ketika pemain menutup prompt undangan.

Parameter

player: Instance

Instansi Player pemain yang menutup prompt.

recipientIds: Array

Tak lagi berpenghuni; array kosong.


PhoneBookPromptClosed

Melepaskan api saat pemain menutup prompt buku telepon.

Parameter

player: Instance

Instansi Player pemain yang menutup buku telepon.


Callback

OnCallInviteInvoked

Panggilan balik untuk diproses saat panggilan dibuat dari buku telepon.Parameter tag dapat digunakan untuk membedakan antara berbagai "poin masuk" atau serupa, seperti yang dijelaskan di PromptPhoneBook() .Hanya satu panggilan balasan yang dapat ditetapkan.

Parameter

tag: string

String untuk membantu membedakan antara berbagai titik masuk buku telepon.

callParticipantIds: Array

Array yang berisi semua pemain yang terlibat dalam panggilan. Panggilan akan selalu menjadi pemain pertama di array.


Memberikan nilai

Tabel termasuk kunci PlaceId dan ReservedServerAccessCode yang nilainya adalah DataModel.PlaceId dan kode akses server yang dikembalikan oleh TeleportService:ReserveServer(), masing-masing.

Contoh Kode

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