Trying to make NPCs from players friends list!

I want to be able to have NPC’s in my game with the players friends list as them. But I have little to no clue on how to do it. For example a player joins in and is surrounded by NPC’s that have the appearance of some random person on there friends list. If anyone has any idea on how to do this i would love to get some help with this. FYI, It’s a R6 game.

Heres a Pic for reference.

Thank you so much for any help,

Limitless

5 Likes

Hey,
This is actually super easy roblox has Player:GetFriendsAsync() this should give you what you are looking for.
Good Luck Tell me if you need extra help :grinning:

3 Likes

Wow thanks bro, ill check it out. And ill let you know if I run in to any problems! :grinning:

1 Like

Where would I put the code used in this case? Because I have a code just don’t know how to connect it to a (body) like a avatar physically in game. How would I go about doing that?

Thanks!

1 Like

No worries! In the link I gave it says “You can use InsertService:LoadAsset to load the assets that are used in the player’s avatar.” This should give you what you are looking for hopefully. Good luck!

1 Like

Holy Crap dude thanks, that helped so much! :grinning:

1 Like

Ok so I’m lost on how to integrate that code in to the other code to find the id and make it in to a model.

Like I want it to make it so it fetch’s the Id from the first code

local Players = game:GetService("Players")

local USERNAME = "Shedletsky"

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

-- First, get the user ID of the player
local userId = Players:GetUserIdFromNameAsync(USERNAME)
-- Then, get a FriendPages object for their friends
local friendPages = Players:GetFriendsAsync(userId)
-- Iterate over the items in the pages. For FriendPages, these
-- are tables of information about the friend, including Username.
-- Collect each username in a table
local usernames = {}
for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end
-- and finally, print!
print("Friends of " .. USERNAME .. ": " .. table.concat(usernames, ", "))



local assetId = 257489726
local InsertService = game:GetService("InsertService")
local success, model = pcall(InsertService.LoadAsset, InsertService, assetId)
if success and model then
	print("Model loaded successfully")
	model.Parent = workspace
else
	print("Model failed to load!")
end

to work with this code

local assetId = 257489726 **<== I want this to change based on what User ID is fetched from the script above.**

local InsertService = game:GetService("InsertService")

local success, model = pcall(InsertService.LoadAsset, InsertService, assetId)

if success and model then

print("Model loaded successfully")

model.Parent = workspace

else

print("Model failed to load!")

end
1 Like

No Problem! Pretty sure this will also help :smirk: aka body colors shirt and pants :wink:

1 Like

you can use random.new(1,amount of friends they have) (assuming they have more than 1 friend might have to have a precaution incase they don’t have a friend) Hopefully this will help you, the code would look something like this:

-- assuming table looks something like this
local friends = {
 bob,
joe,
mama,
bartholomewjohnsonthethird,
edgykid101
}
--lets say random picked the 1st friend
for i,v in pairs(friends) do
   if i == result then -- result being the result from random.new
   local friendsss = getUserIdFromUsername(v)
    return friendsss -- or local friendid = friendsss
   end
end

Sorry If it’s messy I have to get ready to go somewhere so this will be last reply for tonight. If you need help in the morning I can still help goodluck.

1 Like

Thanks so much if I run in to any problems ill be happy to let you know, enjoy the rest of your night. Be save man peace.

1 Like