:GetFriendsAsync() Error

Heya!

I have an error which is:

Players.ScxiptedShark.PlayerScripts.LocalScript:29: Players.ScxiptedShark.PlayerScripts.LocalScript:16: attempt to call missing method ‘GetCurrentPage’ of string

I cant seem to find the error. My local script is in StarterPlayerScripts and here is my code which i totally did not rip from the devforum (thanks @Vanniris)

local Players = game:GetService(“Players”)
local UserID = Players.LocalPlayer.UserId

local success, friendPages = pcall(function()
return Players:GetFriendsAsync(UserID)
end)

if not success then
– remember to handle pcall errors
end

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 += 1
end
end)
end

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

local randomFriend = userIds[math.random(1, #userIds)]
local newFriend = Players:CreateHumanoidModelFromUserId(randomFriend)
newFriend.Parent = game.Workspace

Please help!

2 Likes

Can you please print out friendPages?

1 Like

ok, need me to send a screenshot or?

1 Like

I still get the same error, this is confusing

1 Like

Yes.

1 Like

i dont get anything
only this error

By the way the error is on line 28

At line 10, please add

print(friendPages)

and send a screenshot of the output.

Are you trying to get all a user’s friends? If you are I typically use this to get all of their friends.

local PlayersService = game:GetService("Players")
local FriendIDs = {}

local success, fail pcall(function()
    local friendsList = PlayersService:GetFriendsAsync(UserId) 

    for page=1,4 do 
        for _,friend in pairs(friendsList:GetCurrentPage()) do 
            table.insert(FriendIDs, friend.Id)
        end

        if page ~= 4 then 
            local pass,err = pcall(function() 
                friendsList:AdvanceToNextPageAsync()
            end)

            if not pass then 
                break 
            end

        end
    end
end)

print(FriendIDs)

1 Like

I am trying to create a character of a random friend from the users friend list and position it to a selected random part.

I cant, i still receive the same error

Please put it before you declare the iterPageItems function.


And then, show the output. C’mon.

wait i think it just worked, it loaded a character

OMG IT RANDOMLY WORKS XD! but there is something else i must ask
how can i make it display the players name?

I do not recommend using CreateHumanoidModelFromUserId() when you could use :ApplyDescription() on an already existing humanoid. That’s something to keep in mind.

task.wait()
local PlayersService = game:GetService("Players")
local LocalPlayer = PlayersService.LocalPlayer
local FriendIDs = {}

local success, fail pcall(function()
	local friendsList = PlayersService:GetFriendsAsync(LocalPlayer.UserId) 

	for page=1,4 do 
		for _,friend in pairs(friendsList:GetCurrentPage()) do 
			table.insert(FriendIDs, friend.Id)
		end

		if page ~= 4 then 
			local pass,err = pcall(function() 
				friendsList:AdvanceToNextPageAsync()
			end)

			if not pass then 
				break 
			end

		end
	end
end)

local Parts = {} -- Your list of parts here

function GetRandomFriend()
	return FriendIDs[math.random(1, #FriendIDs)]
end

local SelectedFriend = GetRandomFriend()
local FriendCharacter = PlayersService:CreateHumanoidModelFromUserId(SelectedFriend)

for i,v in pairs(Parts) do
	v.CanCollide = false
end

FriendCharacter:PivotTo(Parts[math.random(1, #Parts)].CFrame)
FriendCharacter.Parent = workspace
1 Like

so would that just create a character at selected part names?

Yes if it is in a local script, if you want to run it from a server script then just delete the local player and put in your own ID.

They dont load in for some reason,
Code:


error: