Sending a Friends List array from a Local Script to a Server Script is not working

  1. What do you want to achieve?
    Return a FriendPages array from a local script to a server script via a remote event and use it inside of a for loop.

  2. What is the issue?
    It will not work. I have tried many solutions, but nothing will work. The main issue is that inside of the local script, the array is perfectly fine and works within a for loop. But after sending the variable with the FriendPages value through the remote event, it acts as an instance and sometimes a nil value.


Server Script

Local Script

Output

  1. What solutions have you tried so far?

Youtube Videos, API, DevForum posts, etc
Specifically, I’ve tried making the friends_list value in the server script to

friend_list = {}

To initiate the variable as an array/table before it is set to the friend list that is fired from the local script via remote event to the script, but that hasn’t worked. I’ve tried other techniques of retrieving the friend list from the user but that hasn’t worked much either. The code I have right now has worked best out of all the solutions I’ve tried, but it is ultimately still not working. Best of the worst type scenario.

I’ve also tried this across bindable events, and that worked perfectly fine. I made another local script, and when the bindable event inside of that script was fired, it would take the friend list from the original local script, and print each value within a for loop. I don’t know why it doesn’t work with the server script and remote event.

If you need anything else within either script or if you want a picture of the explorer so that you can help me out, then feel free to ask.

onserverevent’s first parameter is the player sending it, so try (plr, friends34) (docs) also, why not use Players:GetFriendsAsync on the server, instead of relying on the client to manage the data (which can be a problem if someone exploits).

I tried this, here’s the results:
It gave the value of friends34 to friends_list, and I know this because I tested friends_list with a for loop like this:

ServerFriendsListReturn.OnServerEvent:Connect(function(plr, friends34)
	friends_list = friends34
	for i, v in pairs(friends_list) do
		print(v)
	end
end)

It ended up printing spot on, and everything worked.

However; for some reason it apparently goes to nil when it encounters the for loop that it needs to be in:

I have already tried this, and it said that I must run Players:GetFriendsAsync on the client.

A: Pages are not tables and connot be iterated as such, the docs has a code example:

Code
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

local userId = 1 -- or any other integer/id

local friendPages = Players:GetFriendsAsync(userId) -- this can be run on the server

local usernames = {}
for item, _pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end

print(table.concat(usernames, ", "))

B: You can run the GetFriendsAsync function from the server, nothing on the docs says it can’t and I have tested it, and you can run it.