GroupService

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
服務
未複製

GroupService 是一種可讓開發人員從遊戲中擷取 Roblox 群組資訊的服務。

基本信息群組,包括它的名稱、說明、所有者、角色和標誌,可以使用 GroupService:GetGroupInfoAsync() 來取得。 列表對一群群的盟友和敵人可以使用 GroupService:GetAlliesAsync()GroupService:GetEnemiesAsync()

GroupService 也可以用於擷取玩家是否為群組的成員,使用 GroupService:GetGroupsAsync() 。 注意,開發人員想要確認玩家是否在群組中,應該使用 Player:IsInGroup() 功能,而不是 GroupService:GetGroupsAsync()

服務有一些有用的應用程式,例如偵測玩家是否為盟友或敵人加入遊戲。

範例程式碼

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

概要

方法

屬性

方法

GetAlliesAsync

暫停

返回包含所有指定群組的友軍資訊的 StandardPages 對象。

這些頁面不包括群組 ID 清單,而是包括群組資訊桌子的清單,這與 GroupService:GetGroupInfoAsync() 返回的格式相同。請參閱下面的表格來了解這些桌子的結構。


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

注意,此功能將返回一個 StandardPages 對象,而不是一個陣列,因此開發人員可能會希望將其轉換為陣列以便使用更方便 (請參閱範例)。

此功能有一些有用的應用程式,包括偵測玩家是否屬於已同盟的群組。

對於敵人,使用 GroupService:GetEnemiesAsync()

參數

groupId: number

群組的ID。


返回

範例程式碼

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

暫停

返回包含所有指定群組敵人資訊的 StandardPages 對象。

這些頁面不包括群組 ID 清單,而是包括群組資訊桌子的清單,這與 GroupService:GetGroupInfoAsync() 返回的格式相同。請參閱下面的表格來了解這些桌子的結構。


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

注意,此功能將返回一個 StandardPages 對象,而不是一個陣列,因此開發人員可能會希望將其轉換為陣列以便使用更方便 (請參閱範例)。

此功能有一些有用的應用程式,包括偵測玩家是否屬於敵人群組。

對於盟友,使用 GroupService:GetAlliesAsync()

參數

groupId: number

群組的ID。


返回

範例程式碼

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
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
暫停

返回包含指定群組資訊的表。

返回的表格與 GroupService:GetAlliesAsync()GroupService:GetEnemiesAsync() 的格式相同。這種格式可以在下面查看。


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

注意,如果群組沒有所有者,所有者會設為零。

這個功能有一些有用的應用程式,包括載入最新的團體描述和標誌,以在群組中顯示。

參數

groupId: number

群組的群組ID。


返回

Variant

一個關於群組的資訊。

範例程式碼

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

暫停

警告: 返回的表中的 IsInClan 屬性將總是返回 錯誤 並且存在於後向兼容性。0>Clans0> 功能已在 Roblox 平台上啓動關閉。

此功能返回包含所有 Player 的表。

返回的列表包含玩家是成員的每個群組的一個項目。這些項目是擁有以下欄位的表。


<tbody>
<tr>
<td><b>名稱</b></td>
<td>群組的名稱</td>
</tr>
<tr>
<td><b>id</b></td>
<td>群組 ID</td>
</tr>
<tr>
<td><b>標誌網址</b></td>
<td>資產網址,連結到群組的預覽圖像 (例如:http://www.roblox.com/asset/?id=276165514)</td>
</tr>
<tr>
<td><b>標誌ID</b></td>
<td>標誌的資產ID,與 EmblemURL 中使用的相同</td>
</tr>
<tr>
<td><b>等級</b></td>
<td>玩家擁有的排名ID (例如: 255 對於所有者)</td>
</tr>
<tr>
<td><b>角色</b></td>
<td>玩家的群組排名 (例如: 群組所有者)</td>
</tr>
<tr>
<td><b>主要是</b></td>
<td>指示此是玩家主要群組的否</td>
</tr>
<tr>
<td><b>IsInClan</b></td>
<td>指示玩家是否在此群組的複製人</td>
</tr>
</tbody>
名稱說明

與 Class.GroupService:GetAlliesAsync() 和 Class.GroupService:GetEnemiesAsync() 不同,GetGroupsAsync 會返回一個表而不是一個 Class.StandardPages 對物件。

參數

userId: number

使用者的 Player.UserId


返回

包含群組的 Player 是一個成員。

範例程式碼

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

活動