How can i generate Random NPCS that are the players friend?

Heya!

I want to create a system where it randomly finds a friend of yours and creates a npc and parents then to a random part. I know i have to use :Getfriendsasync() but im not sure how to use it.

Here is an example of what im trying to create:

Thanks!

-ScxiptedShark

3 Likes
3 Likes

This is still really confusing to me.

I don’t know how i would structure the code

1 Like

You will not be able to access any player’s avatar that is offline. However, you can do it if you’re familiar with HTTPService or other programming languages that would allow you to get the player’s avatar items.

I will give you a quick introduction, there is something called API, which is basically communication between applications, and in Roblox you can access the avatar items of a player even if they were offline. But depending on your coding experience that would take time and effort to achieve, APIs are so easy but sometimes HTTPService does not allow you to request from Roblox itself, so you will need to use a “proxy” A proxy is an outside app/code that will get the player’s avatar items and then you will request it from roblox studio.

Read HTTPService documents for more information

The url in which you will get the avatar items is

https://avatar.roblox.com/v1/users/{UserId}/avatar

an example of this is

https://avatar.roblox.com/v1/users/494096249/avatar

These are my avatar items and you can view every detail about it (name, assetid, etc)

Summary: You’ll need to use outside code to get the player avatar items and then request it from studio, then update the rig’s avatar items. This requires effort and time and is suggested to be done with a person who knows at least 2 coding languages and is good with APIs.

(I recommend using python at replit, fastest and easiest proxy to make)

Good luck

3 Likes

Yeah…

I will just create the characters myself and clone them lol

Seems a bit difficult to understand for a small brain like mine

I’m pretty sure you can get better at these topics, APIs are really easy and interesting to use. Try to learn them if possible. No one started good at it but if you get deeper into the topic you’ll understand it eventually.

1 Like

Is there not a way where i can just use the offline friends?

After searching more on the devforum it seems i don’t have to worry about that

There is even a doccumentation on :GetFriendAsync() which seems to have no issues with online friends, i just struggle to integrate it

oh lol that’s true, I should’ve looked a bit on the documentations.

I mean you can look at this doc

game.Players:CreateHumanoidModelFromDescription(game.Players:GetHumanoidDescriptionFromUserId(1), Enum.HumanoidRigType.R15).Parent = game.Workspace looks like this work

1 Like
> local Player = game.Players.LocalPlayer
> local Players = game:GetService("Players")
> 
> 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 friendPages = Players:GetFriendsAsync(Player.UserId)
> 
> local usernames = {}
> 
> for item, _pageNo in iterPageItems(friendPages) do
> 	table.insert(usernames, item.Username)
> 	game.Players:CreateHumanoidModelFromDescription(game.Players:GetHumanoidDescriptionFromUserId(u

sernames), Enum.HumanoidRigType.R15).Parent = game.Workspace
end

How can i make it so it loads a certain amount of friends avatars? And how do i load the friends avatar?

You can get the friends from GetFriendsAsync, which returns a pages. Within that, you can loop through all of the pages and append their user IDs to a table.

Then, you can pick a few from that table and load their humanoid description onto a blank NPC.

1 Like

I have looped through and got the friends with :GetFriendsAsync(), but how would i make it select like 5?

You would pick them from the list you’ve created earlier.

local list = {} --the friends list
local objects = {} --output
local randoms = {}
local count = 5
for i = 1, count do
	table.insert(randoms, math.random(1, #list))
end
for i = 1, #list do
	if table.find(randoms, i) then
		table.insert(objects, list[i])
	end
end
print(objects)

edit: used table.find

1 Like

Heres what i understand:

Count = amount of NPC’S i want to spawn.

If i replace

> local friendPages = Players:GetFriendsAsync(Player.UserId)
> 
> local usernames = {}
> 
> for item, _pageNo in iterPageItems(friendPages) do
> 	table.insert(usernames, item.Username)
> 	game.Players:CreateHumanoidModelFromDescription(game.Players:GetHumanoidDescriptionFromUserId(u

with your code, it will work?

No, my code expects there to be a usernames field. You must use usernames as the list table in mine.

1 Like

ok so would this work:

local Player = game.Players.LocalPlayer
	 local Players = game:GetService("Players")
	 
	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 friendPages = Players:GetFriendsAsync(Player.UserId)
local usernames = {}
local objects = {} --output
local randoms = {}
local count = 5

for i = 1, count do
	table.insert(randoms, math.random(1, #usernames))
end
for i = 1, #usernames do
	if table.find(randoms, i) then
		table.insert(objects, usernames[i])
	end
end
print(objects)

You have to change this and list to usernames

1 Like

i just noticed that lol, ill test it out!

Error:

Players.ScxiptedShark.PlayerGui.LocalScript:29: invalid argument #2 to ‘random’ (interval is empty)

That means there are no items in the list.

1 Like