GroupService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
GroupService เป็นบริการที่ช่วยให้นักพัฒนาสามารถดึงข้อมูลเกี่ยวกับกลุ่ม Roblox จากภายในเกมได้
ข้อมูลพื้นฐานเกี่ยวกับกลุ่ม รวมถึงชื่อ คำอธิบาย เจ้าของ บทบาท และตราสัญลักษณ์สามารถดึงได้โดยใช้ GroupService:GetGroupInfoAsync()รายชื่อพันธมิตรและศัตรูของกลุ่มสามารถดึงได้โดยใช้ GroupService:GetAlliesAsync() และ GroupService:GetEnemiesAsync()
บริการกลุ่มยังสามารถใช้เพื่อดึงรายการกลุ่มที่ผู้เล่นเป็นสมาชิกได้โดยใช้ GroupService:GetGroupsAsync()โปรดทราบว่านักพัฒนาที่ต้องการตรวจสอบว่าผู้เล่นอยู่ในกลุ่มควรใช้ฟังก์ชัน Player:IsInGroup() แทนที่จะใช้ GroupService:GetGroupsAsync()
บริการมีจำนวนแอปพลิเคชันที่มีประโยชน์ เช่น การตรวจสอบว่าผู้เล่นเป็นพันธมิตรหรือศัตรูเมื่อเข้าร่วมเกม
ตัวอย่างโค้ด
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.
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
สรุป
วิธีการ
คืนวัตถุ StandardPages รวมถึงข้อมูลเกี่ยวกับพันธมิตรของกลุ่มที่กำหนดทั้งหมด
คืนวัตถุ StandardPages รวมถึงข้อมูลเกี่ยวกับศัตรูของกลุ่มที่กำหนดทั้งหมด
คืนตารางที่มีข้อมูลเกี่ยวกับกลุ่มที่กำหนดให้
คืนรายการของตารางที่มีข้อมูลเกี่ยวกับกลุ่มทั้งหมดที่ผู้เล่นที่กำหนดให้เป็นสมาชิก
คุณสมบัติ
วิธีการ
GetAlliesAsync
คืนวัตถุ StandardPages รวมถึงข้อมูลเกี่ยวกับพันธมิตรของกลุ่มที่กำหนดทั้งหมด
หน้านี้ไม่รวมรายการของรหัสกลุ่ม แต่รวมรายการของตารางข้อมูลกลุ่มซึ่งสะท้อนรูปแบบที่กลับโดย 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()
พารามิเตอร์
ID ของกลุ่ม
ส่งค่ากลับ
ตัวอย่างโค้ด
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.
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 รวมถึงข้อมูลเกี่ยวกับศัตรูของกลุ่มที่กำหนดทั้งหมด
หน้านี้ไม่รวมรายการของรหัสกลุ่ม แต่รวมรายการของตารางข้อมูลกลุ่มซึ่งสะท้อนรูปแบบที่กลับโดย 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()
พารามิเตอร์
ID ของกลุ่ม
ส่งค่ากลับ
ตัวอย่างโค้ด
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.
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
คืนตารางที่มีข้อมูลเกี่ยวกับกลุ่มที่กำหนดให้
ตารางที่คืนมามีรูปแบบเดียวกับที่คืนใน 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}}}
โปรดทราบว่าหากกลุ่มไม่มีเจ้าของ ฟิลด์เจ้าของจะถูกตั้งเป็น nil
ฟังก์ชันนี้มีจำนวนแอปที่มีประโยชน์ รวมถึงการโหลดคำอธิบายและโลโก้ล่าสุดของกลุ่มเพื่อแสดงในฐานกลุ่ม
พารามิเตอร์
รหัสกลุ่มของกลุ่ม
ส่งค่ากลับ
สารานุกรมของข้อมูลเกี่ยวกับกลุ่ม
ตัวอย่างโค้ด
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.
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 ในตารางที่คืนจะเสมอกลับ ปลอม และมีอยู่เพื่อการย้อนกลับความสอดคล้องคุณลักษณะของตระกูลถูกยกเลิกในปี 2016 จากแพลตฟอร์ม Roblox
ฟังก์ชันนี้จะคืนรายการของตารางที่มีข้อมูลเกี่ยวกับกลุ่มทั้งหมดที่สมาชิกให้ Player เป็นสมาชิก
รายการที่ส่งคืนจะรวมถึงรายการสำหรับทุกกลุ่มที่ผู้เล่นเป็นสมาชิก รายการเหล่านี้เป็นตารางที่มีฟิลด์ต่อไปนี้
<th>คําอธิบาย</th></tr></thead><tbody><tr><td><b>ชื่อ</b></td><td>ชื่อกลุ่ม</td></tr><tr><td><b>Id</b></td><td>ID กลุ่ม</td></tr><tr><td><b>ที่อยู่ URL สัญลักษณ์</b></td><td>ลิงค์ที่เชื่อมโยงไปยังภาพรวมของกลุ่ม (ตัวอย่างเช่น: http://www.roblox.com/asset/?id=276165514)</td></tr><tr><td><b>รหัสสัญลักษณ์</b></td><td>assetId ของตราสัญลักษณ์ซึ่งเป็นเช่นเดียวกับที่ใช้ใน EmblemUrl</td></tr><tr><td><b>อันดับ</b></td><td>อันดับที่ผู้เล่นมี (ตัวอย่างเช่น: 255 สำหรับเจ้าของ)</td></tr><tr><td><b>บทบาท</b></td><td>ชื่อกลุ่มของผู้เล่น (ตัวอย่างเช่น: เจ้าของกลุ่ม)</td></tr><tr><td><b>เป็นหลัก</b></td><td>บูลีนที่บ่งบอกว่านี่คือกลุ่มหลักของผู้เล่น</td></tr><tr><td><b>อยู่ในแคลน</b></td><td>บูลีนที่บ่งบอกว่าผู้เล่นอยู่ในกลุ่มของตระกูลนี้</td></tr></tbody>
ชื่อ |
---|
หมายเหตุ ไม่เหมือนกับ GroupService:GetAlliesAsync() และ GroupService:GetEnemiesAsync() , GetGroupsAsync คืนตารางแทนที่จะเป็นวัตถุ StandardPages
พารามิเตอร์
The Player.UserId ของผู้ใช้
ส่งค่ากลับ
ตัวอย่างโค้ด
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().
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