GroupService

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

GroupService adalah layanan yang memungkinkan pengembang untuk mengambil informasi tentang grup Roblox dari dalam game.

Informasi dasar tentang grup, termasuk namanya, deskripsi, pemilik, peran, dan lambang dapat diambil menggunakan GroupService:GetGroupInfoAsync() .Daftar sekutu dan musuh grupdapat diambil menggunakan GroupService:GetAlliesAsync() dan GroupService:GetEnemiesAsync() .

GroupService juga dapat digunakan untuk mengambil daftar grup yang anggotanya adalah pemain, menggunakan GroupService:GetGroupsAsync() .Catatan, pengembang yang ingin memeriksa apakah pemain berada dalam kelompok harus menggunakan fungsi Player:IsInGroup() daripada GroupService:GetGroupsAsync().

Layanan memiliki beberapa aplikasi berguna, seperti mendeteksi apakah pemain adalah sekutu atau musuh saat bergabung dengan game.

Contoh Kode

This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.

Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.

Group Ally/Enemy Checker

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end

Rangkuman

Metode

Properti

Metode

GetAlliesAsync

Hasil

Kembalikan objek StandardPages termasuk informasi tentang semua sekutu grupyang ditentukan.

Halaman ini tidak termasuk daftar ID grup tetapi daftar tabel informasi grup, mencerminkan format yang dikembalikan oleh GroupService:GetGroupInfoAsync() .Lihat di bawah ini untuk struktur tabel ini.


group = {
Name = "Knights of the Seventh Sanctum",
Id = 377251,
Owner = {
Name = "Vilicus",
Id = 23415609
},
EmblemUrl = "http://www.roblox.com/asset/?id=60428602",
Description = "We fight alongside the balance to make sure no one becomes to powerful",
Roles = {
[1] = {
Name = "Apprentice",
Rank = 1
},
[2] = {
Name = "Warrior",
Rank = 2
},
[3] = {
Name = "Earth Walker",
Rank = 255
}
}
}

Perhatikan, karena fungsi ini mengembalikan objek StandardPages sebagai gantinya bukan array, pengembang mungkin ingin mengubahnya menjadi array untuk kemudahan penggunaan (lihat contoh).

Fungsi ini memiliki beberapa aplikasi berguna, termasuk mendeteksi apakah pemain adalah anggota grupsekutu.

Untuk musuh, gunakan GroupService:GetEnemiesAsync() .

Parameter

groupId: number

ID grup.

Nilai Default: ""

Memberikan nilai

Contoh Kode

GroupService:GetAlliesAsync

local Players = game:GetService("Players")
local GroupService = game:GetService("GroupService")
local GROUP_ID = 57
-- creates a table of all of the allies of a given group
local allies = {}
local pages = GroupService:GetAlliesAsync(GROUP_ID)
while true do
for _, group in pairs(pages:GetCurrentPage()) do
table.insert(allies, group)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
function onPlayerAdded(player)
for _, group in pairs(allies) do
if player:IsInGroup(group.Id) then
print("Player is an ally!")
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- handle players who joined while the allies list was still loading
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end

This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.

Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.

Group Ally/Enemy Checker

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end

GetEnemiesAsync

Hasil

Kembalikan objek StandardPages termasuk informasi tentang semua musuh grupyang ditentukan.

Halaman ini tidak termasuk daftar ID grup tetapi daftar tabel informasi grup, mencerminkan format yang dikembalikan oleh GroupService:GetGroupInfoAsync() .Lihat di bawah ini untuk struktur tabel ini.


group = {
Name = "Knights of the Seventh Sanctum",
Id = 377251,
Owner = {
Name = "Vilicus",
Id = 23415609
},
EmblemUrl = "http://www.roblox.com/asset/?id=60428602",
Description = "We fight alongside the balance to make sure no one becomes to powerful",
Roles = {
[1] = {
Name = "Apprentice",
Rank = 1
},
[2] = {
Name = "Warrior",
Rank = 2
},
[3] = {
Name = "Earth Walker",
Rank = 255
}
}
}

Perhatikan, karena fungsi ini mengembalikan objek StandardPages sebagai gantinya bukan array, pengembang mungkin ingin mengubahnya menjadi array untuk kemudahan penggunaan (lihat contoh).

Fungsi ini memiliki beberapa aplikasi berguna, termasuk mendeteksi apakah pemain adalah anggota grupmusuh.

Untuk sekutu, gunakan GroupService:GetAlliesAsync() .

Parameter

groupId: number

ID grup.

Nilai Default: ""

Memberikan nilai

Contoh Kode

GroupService:GetEnemiesAsync

local Players = game:GetService("Players")
local GroupService = game:GetService("GroupService")
local GROUP_ID = 57
-- creates a list of all of the enemies of a given group
local enemies = {}
local pages = GroupService:GetEnemiesAsync(GROUP_ID)
while true do
for _, group in pairs(pages:GetCurrentPage()) do
table.insert(enemies, group)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
function onPlayerAdded(player)
for _, enemyGroup in pairs(enemies) do
if player:IsInGroup(enemyGroup.Id) then
print("Player is an enemy!")
break
end
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
-- handle players who joined while the enemies list was still loading
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end

This code sample demonstrates how GroupService and Player:IsInGroup() can be used to determine whether a player is a member of a group, or any of its allies or enemies.

Note as GroupService:GetAlliesAsync() and GroupService:GetEnemiesAsync() use StandardPages objects a utility function is used to convert them to allies.

Group Ally/Enemy Checker

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
-- define group id here
local GROUP_ID = 271454
-- utility function for dealing with pages
local function pagesToArray(pages)
local array = {}
while true do
for _, v in ipairs(pages:GetCurrentPage()) do
table.insert(array, v)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
end
return array
end
-- get lists of allies and enemies
local alliesPages = GroupService:GetAlliesAsync(GROUP_ID)
local enemiesPages = GroupService:GetEnemiesAsync(GROUP_ID)
-- convert to array
local allies = pagesToArray(alliesPages)
local enemies = pagesToArray(enemiesPages)
local function playerAdded(player)
-- check to see if the player is in the group
if player:IsInGroup(GROUP_ID) then
print(player.Name .. " is a member!")
else
local isAlly, isEnemy = false, false
-- check to see if the player is in any ally groups
for _, groupInfo in ipairs(allies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isAlly = true
break
end
end
-- check to see if the player is in any enemy groups
for _, groupInfo in ipairs(enemies) do
local groupId = groupInfo.Id
if player:IsInGroup(groupId) then
isEnemy = true
break
end
end
if isAlly and not isEnemy then
print(player.Name .. " is an ally!")
elseif isEnemy and not isAlly then
print(player.Name .. " is an enemy!")
elseif isEnemy and isAlly then
print(player.Name .. " is both an ally and an enemy!")
else
print(player.Name .. " is neither an ally or an enemy!")
end
end
end
-- listen for new players being added
Players.PlayerAdded:Connect(playerAdded)
-- handle players already in game
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end

GetGroupInfoAsync

Variant
Hasil

Kembalikan tabel yang berisi informasi tentang grupyang diberikan.

Tabel yang dikembalikan adalah format yang sama dengan yang dikembalikan di GroupService:GetAlliesAsync() dan GroupService:GetEnemiesAsync() . Format ini dapat dilihat di bawah ini.


group = {
Name = "Knights of the Seventh Sanctum",
Id = 377251,
Owner = {
Name = "Vilicus",
Id = 23415609
},
EmblemUrl = "http://www.roblox.com/asset/?id=60428602",
Description = "We fight alongside the balance to make sure no one becomes to powerful",
Roles = {
[1] = {
Name = "Apprentice",
Rank = 1
},
[2] = {
Name = "Warrior",
Rank = 2
},
[3] = {
Name = "Earth Walker",
Rank = 255
}
}
}

Catatan, jika sebuah kelompok tidak memiliki pemilik, bidang Pemilik akan diatur ke nil .

Fungsi ini memiliki beberapa aplikasi berguna, termasuk memuat deskripsi dan logo terbaru dari sebuah grup untuk ditampilkan di basis grup.

Parameter

groupId: number

ID grup dari grup.

Nilai Default: ""

Memberikan nilai

Variant

Kamus informasi tentang grup.

Contoh Kode

GroupService:Dapatkan Info Grup Asynchronous

local GroupService = game:GetService("GroupService")
local GROUP_ID = 377251
local group = GroupService:GetGroupInfoAsync(GROUP_ID)
print(group.Name .. " has the following roles:")
for _, role in ipairs(group.Roles) do
print("Rank " .. role.Rank .. ": " .. role.Name)
end

The code in this sample spawns a Part in the Workspace that includes a texture of the given group's emblem.

Load Group Emblem

local GroupService = game:GetService("GroupService")
local function getEmblemAsync(groupId)
local groupInfo = GroupService:GetGroupInfoAsync(groupId)
return groupInfo.EmblemUrl
end
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(5, 5, 1)
part.Position = Vector3.new(0, 5, 0)
local decal = Instance.new("Decal")
decal.Parent = part
part.Parent = workspace
decal.Texture = getEmblemAsync(377251)

GetGroupsAsync

Hasil

Peringatan: Properti IsInClan di tabel yang dikembalikan akan selalu mengembalikan false dan ada untuk kompatibilitas mundur.Fitur Klan adalah matahari terbenam dari platform Roblox pada 2016.

Fungsi ini men返回列 daftar tabel yang berisi informasi tentang semua kelompok yang diberikan Player adalah anggota.

Daftar yang dikembalikan akan mencakup entri untuk setiap kelompok yang anggotanya adalah pemain. Entri ini adalah tabel dengan bidang berikut.


<th>Deskripsi</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Nama</b></td>
<td>Nama grup</td>
</tr>
<tr>
<td><b>Id</b></td>
<td>ID kelompok</td>
</tr>
<tr>
<td><b>EmblemUrl</b></td>
<td>Tautan url aset yang menautkan ke thumbnail grup (misalnya: http://www.roblox.com/asset/?id=276165514)</td>
</tr>
<tr>
<td><b>Id Lambang</b></td>
<td>AssetId dari emblem, yang sama yang digunakan di EmblemUrl</td>
</tr>
<tr>
<td><b>Peringkat</b></td>
<td>RankId yang dimiliki pemain (misalnya: 255 untuk pemilik)</td>
</tr>
<tr>
<td><b>Peran</b></td>
<td>Nama grouprank pemain (misalnya: Pemilik Grup)</td>
</tr>
<tr>
<td><b>Apakah Primary</b></td>
<td>A boolean yang menunjukkan apakah ini adalah gruputama pemain</td>
</tr>
<tr>
<td><b>Adalah di Clan IsInClan</b></td>
<td>Boolean yang menunjukkan apakah pemain berada di klan grupini</td>
</tr>
</tbody>
Nama

Perhatikan tidak seperti GroupService:GetAlliesAsync() dan GroupService:GetEnemiesAsync() , GetGroupsAsync mengembalikan tabel daripada objek StandardPages.

Parameter

userId: number

The Player.UserId dari pengguna.

Nilai Default: ""

Memberikan nilai

Sebuah array kamus yang berisi informasi tentang grupPlayer adalah anggota.

Contoh Kode

This code sample will print information on all of the groups a player is a member of when they join the game, using GroupService:GetGroupsAsync().

Getting the Groups that a User is A Member Of

local GroupService = game:GetService("GroupService")
local Players = game:GetService("Players")
local function playerAdded(player)
-- load a list of info on all groups the player is a member of
local groups = GroupService:GetGroupsAsync(player.UserId)
for _, groupInfo in pairs(groups) do
for key, value in pairs(groupInfo) do
print(key .. ": " .. tostring(value))
end
print("--")
end
end
Players.PlayerAdded:Connect(playerAdded)
-- go through existing players
for _, player in pairs(Players:GetPlayers()) do
playerAdded(player)
end

Acara