範例程式碼
列印 Roblox 朋友
local Players = game:GetService("Players")
local USERNAME = "Cozecant"
local function iterPageItems(pages)
return coroutine.wrap(function()
local pagenum = 1
while true do
for _, item in ipairs(pages:GetCurrentPage()) do
coroutine.yield(item, pagenum)
end
if pages.IsFinished then
break
end
pages:AdvanceToNextPageAsync()
pagenum = pagenum + 1
end
end)
end
-- 首先,獲取玩家的使用者 ID
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- 然後,為他們的朋友獲取一個 FriendPages 對象
local friendPages = Players:GetFriendsAsync(userId)
-- 遍歷頁面中的項目。對於 FriendPages,這些
-- 是有關朋友的資訊表,包括使用者名稱。
-- 在一個表中收集每個使用者名稱
local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
table.insert(usernames, item.Username)
end
print("連線的 " .. USERNAME .. ": " .. table.concat(usernames, ", "))