GroupService

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Không Thể Tạo
Dịch Vụ
Không Sao Chép

GroupService là một dịch vụ cho phép các nhà phát triển truy cập thông tin về một nhóm Roblox từ bên trong trò chơi.

Thông tin cơ bản về nhóm, bao gồm tên, mô miêu tả, chủ sở hữu, vai trò và biểu tượng có thể được truy cập bằng cách sử dụng GroupService:GetGroupInfoAsync() .Danh sách đồng minh và kẻ thù của một nhóm có thể được truy cập bằng cách sử dụng GroupService:GetAlliesAsync()GroupService:GetEnemiesAsync() .

GroupService cũng có thể được sử dụng để lấy danh sách các nhóm mà một người chơi là thành viên, bằng cách sử dụng GroupService:GetGroupsAsync() .Lưu ý, các nhà phát triển muốn kiểm tra xem một người chơi có ở trong nhóm hay không nên sử dụng chức năng Player:IsInGroup() thay vì GroupService:GetGroupsAsync() .

Dịch vụ có một số ứng dụng hữu ích, chẳng hạn như phát hiện xem một người chơi có phải là đồng minh hay kẻ thù khi tham gia trò chơi.

Mẫu mã

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

Tóm Tắt

Phương Pháp

Thuộc Tính

Phương Pháp

GetAlliesAsync

Sinh Lợi

Trả về một đối tượng StandardPages bao gồm thông tin về tất cả các đồng minh của nhóm được chỉ định.

Các trang này không bao gồm một danh sách các ID nhóm nhưng thay vào đó là một danh sách các bảng thông tin nhóm, phản ánh định dạng của những gì được trả về bởi GroupService:GetGroupInfoAsync() .Xem phía dưới để có cấu trúc của các bảng này.


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
}
}
}

Lưu ý, vì chức năng này trả về một đối tượng StandardPages thay vì một mảng, các nhà phát triển có thể muốn chuyển đổi nó thành một mảng để dễ sử dụng (xem ví dụ).

Chức năng này có một số ứng dụng hữu ích, bao gồm việc phát hiện xem một người chơi có phải là thành viên của một nhóm đồng minh hay không.

Đối với kẻ thù, sử dụng GroupService:GetEnemiesAsync() .

Tham Số

groupId: number

ID của nhóm.

Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

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

Sinh Lợi

Trả về một đối tượng StandardPages bao gồm thông tin về tất cả các kẻ thù của nhóm được chỉ định.

Các trang này không bao gồm một danh sách các ID nhóm nhưng thay vào đó là một danh sách các bảng thông tin nhóm, phản ánh định dạng của những gì được trả về bởi GroupService:GetGroupInfoAsync() .Xem phía dưới để có cấu trúc của các bảng này.


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
}
}
}

Lưu ý, vì chức năng này trả về một đối tượng StandardPages thay vì một mảng, các nhà phát triển có thể muốn chuyển đổi nó thành một mảng để dễ sử dụng (xem ví dụ).

Chức năng này có một số ứng dụng hữu ích, bao gồm việc phát hiện xem một người chơi có phải là thành viên của một nhóm địch hay không.

Đối với đồng minh, sử dụng GroupService:GetAlliesAsync() .

Tham Số

groupId: number

ID của nhóm.

Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

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
Sinh Lợi

Trả về một bảng chứa thông tin về nhóm đã cho.

Bảng trả về có cùng định dạng như được trả về trong GroupService:GetAlliesAsync()GroupService:GetEnemiesAsync() . Định dạng này có thể được xem dưới đây.


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
}
}
}

Lưu ý, nếu một nhóm không có chủ sở hữu, trường Chủ sở hữu sẽ được đặt thành nil .

Chức năng này có một số ứng dụng hữu ích, bao gồm việc tải mô tả và logo mới nhất của một nhóm để hiển thị trong cơ sở nhóm.

Tham Số

groupId: number

ID nhóm của nhóm.

Giá Trị Mặc Định: ""

Lợi Nhuận

Variant

Một từ điển thông tin về nhóm.

Mẫu mã

GroupService:GetGroupInfoAsync

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

Sinh Lợi

Cảnh báo: Thuộc tính IsInClan trong bảng trả về sẽ luôn luôn trả về giả mạo và tồn tại để tương thích ngược.Tính năng Gia tộc đã lặng sóng từ nền tảng Roblox vào năm 2016.

Chức năng này trả về một danh sách các bảng chứa thông tin về tất cả các nhóm mà một Player nhất định là thành viên.

Danh sách trả về sẽ bao gồm một cái ghi cho mỗi nhóm mà người chơi là thành viên. Các ghi này là các bảng với các trường sau.


<th>Mô tả</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Tên</b></td>
<td>Tên nhóm</td>
</tr>
<tr>
<td><b>Id</b></td>
<td>ID nhóm</td>
</tr>
<tr>
<td><b>URL biểu tượng</b></td>
<td>Một liên kết URL tài sản dẫn đến hình ảnh thumbnail của nhóm ( ví dụ: http://www.roblox.com/asset/?id=276165514)</td>
</tr>
<tr>
<td><b>ID Huy hiệu</b></td>
<td>ID tài sản của huy hiệu, giống như được sử dụng trong EmblemUrl</td>
</tr>
<tr>
<td><b>Xếp hạng</b></td>
<td>RankId mà người chơi có (ví dụ: 255 cho chủ sở hữu)</td>
</tr>
<tr>
<td><b>Nhóm vai trò</b></td>
<td>Tên của grouprank của người chơi ( ví dụ: Chủ nhóm )</td>
</tr>
<tr>
<td><b>Là chính</b></td>
<td>Một boolean chỉ ra nếu đây là nhóm chính của người chơi</td>
</tr>
<tr>
<td><b>Có trong clan IsInClan</b></td>
<td>Một boolean chỉ ra nếu người chơi ở trong clan của nhóm này</td>
</tr>
</tbody>
Tên

Lưu ý không giống như GroupService:GetAlliesAsync()GroupService:GetEnemiesAsync(), GetGroupsAsync trả về một bảng thay vì một đối tượng StandardPages.

Tham Số

userId: number

The Player.UserId của người dùng.

Giá Trị Mặc Định: ""

Lợi Nhuận

Một mảng các từ điển chứa thông tin về nhóm mà Player là thành viên.

Mẫu mã

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

Sự Kiện