Attempt to iterate over a nil value

I’m trying to pull the local player’s friends using the :GetFriendsAsync() function and then load their characters, but whenever I iterate through the page numbers I get the error shown in the title.

Here is my script

local Players = game:GetService("Players")


local function iteratePageNumbers(Pages)
	coroutine.wrap(function()
		local pagenum = 1
		while task.wait() do
			for _, item in ipairs(Pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			
			if Pages.IsFinished then 
				break
			end
			Pages:AdvanceToNextAsync()
			pagenum += 1
			
		end
	end)
end
	

local player = Players.LocalPlayer
local userId = player.UserId
local friendPages = Players:GetFriendsAsync(userId)
print(friendPages)

local friendsUsers = {}

--error right here 
for item, pgNum in iteratePageNumbers(friendPages) do
	table.insert(friendsUsers, item.Username)
	
end

print("Friends of " .. player.Name .. ": " .. table.concat(friendPages, ","))

This is basically the same script used in this documentation Players | Documentation - Roblox Creator Hub, but instead of getting the friends from a given user, I’m trying to get the friends from the local players and I’m getting an error. It could be a simple error but idk

its because the iteratePageNumbers doesnt return anything so the loop that errors has nothing to iterate over

1 Like

So would I return the page number then? How would I write this I tried adding returns and I get the same error.

1 Like

try doing it like the other script does, just return the coroutine.wrap
image

1 Like

Oops I spent all this time just to realize I didn’t even copy down the script correctly. Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.