How do you get a player's friends on a R6 Rig?

If you see some tycoons they will use the players friends as the workers for the game. I want to know exactly how you do that! Like when they claim the tycoon everytime they get a worker the players friend will be randomized.

2 Likes

Use this to get the players friend info and load there avatar with the players friend UserID

2 Likes

Im a bit confused here, I assume you get the pageinstance and then get the humanoid description and put it in the dummer, userid is unkown global (I havent used userid too much) and Im a bit confused

2 Likes

Using the Players service that @WizulousThe2nd sent, you can get all the friends of the player using the function Players:GetFriendsAsync() and putting in the userid of the player, which you can also get by using Players:GetUserIdFromNameAsync() and putting in the username of the player you want to get. You can use a for loop to loop through all of the friends that the former function returns, and get their Humanoid Descriptions (by using function Players:GetHumanoidDescriptionFromUserId()) to use function (dummy humanoid)Humanoid:ApplyDescription() and put the humanoid description from the previous step into the function.

2 Likes
local Players = game:GetService("Players")
local Humanoid = script.Parent.Humanoid
local friends = Players:GetFriendsAsync(3290367697)

for i, v in pairs(friends) do
	Humanoid:ApplyDescription()
end

I know this is sorta wrong but tell me what I have to fix/do

2 Likes

Let’s get the easy stuff out of the way:
In the for loop, user ipairs() instead of pairs() because you are iterating through a table.
in your Humanoid:ApplyDescription(), you are missing the actual description. Use Players:GetHumanoidDescriptionFromUserId(v.UserId) as your argument, because the function gets the humanoid description from the friends’ userid.

I would personally have a different implementation, because I am assuming that there are more than one upgrades that use your friend as a worker. In this case, I would put this script inside of the entrance door to the tycoon (the one where you claim the tycoon) and put in all of the humanoids that the player will eventually unlock inside of the script. I would also have two different indexes for the two different values. I revised your script (quite heavily honestly) and changed it, as my initial implementation was just wrong

local Players = game:GetService("Players")
local Humanoid1 = script.Parent.Parent.Upgrade1.Dummy.Humanoid
local Humanoid2 = script.Parent.Parent.Upgrade2.Dummy.Humanoid
local Humanoid3 = script.Parent.Parent.Upgrade3.Dummy.Humanoid -- placeholders for what you actually have
local humlist = {Humanoid1, Humanoid2, Humanoid3} -- this is for looping through
local friends = Players:GetFriendsAsync(3290367697)
local friendsindex = 1 -- indexes (if the player has less friends than dummies)

for i, v in ipairs(humlist) do
    if i > #friends then -- if player has less friends than dummies
        friendsindex = 1 -- just reset the counter of friends and use same friends over and over again
    end
    v:ApplyDescription(Players:GetHumanoidDescriptionFromUserId(friends[friendsindex].UserId))
    friendsindex += 1 -- next friend in list
end
3 Likes

Thanks! I will try this later and see if it works. Most likely I might make it a module script so the dummy will have neat, easy, and simple code but I might have to think more into that.

Workspace.Rig.Script:10: attempt to get length of a Instance value is the error i got. How should I fix this? it is the line that says if “i>#friends then”

Where did you put the script? I wrote the script to work from the starter door (when you claim the tycoon slot)

I also think I made a mistake with the reading of all of the friend values. I initially thought that Players:GetFriendsAsync() returned a table instead of a “FriendPages” object.

After doing a quick google search (lol), revise the entire script to:

local Players = game:GetService("Players")
local Humanoid1 = script.Parent.Parent.Upgrade1.Dummy.Humanoid
local Humanoid2 = script.Parent.Parent.Upgrade2.Dummy.Humanoid
local Humanoid3 = script.Parent.Parent.Upgrade3.Dummy.Humanoid -- placeholders for what you actually have
local humlist = {Humanoid1, Humanoid2, Humanoid3} -- this is for looping through
local friends = Players:GetFriendsAsync(3290367697)
local friendsindex = 1 -- indexes (if the player has less friends than dummies)

local function iterPageItems(pages) -- i found this function off of the documentation, it's supposed to take the "FriendPages" object and split it into an iterable dictionary.
	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 useridfriends = {} -- empty list to fill later
for item, pagenumber in iterPageItems(friends) do -- loops through all of the pages decompiled by the function that we defined earlier
    table.insert(useridfriends, item.Id) -- takes the id and adds it to table useridfriends
end

for i, v in ipairs(humlist) do
    if i > #useridfriends then -- if player has less friends than dummies
        friendsindex = 1 -- just reset the counter of friends and use same friends over and over again
    end
    v:ApplyDescription(Players:GetHumanoidDescriptionFromUserId(useridfriends[friendsindex]))
    friendsindex += 1 -- next friend in list
end

Sorry about the misjudgement, lmk if this script has any issues. There may be some syntax errors but probably nothing too bad.

btw, i got the iterPageItems function from this link (FriendPages documentation)

1 Like

Thank you so much! This helped me alot! :grin:

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