Need help defaulting a UserId if the player has no friends

Hello!

My local script in StarterPlayerScripts,

local plr = game.Players.LocalPlayer
local folder = game.ReplicatedStorage:WaitForChild("NPC")

local friends = game.Players:GetFriendsAsync(plr.UserId)
local usernames = {} 
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

for item, pageNo in iterPageItems(friends) do 
	table.insert(usernames, item.Username)
end

local folderItems = folder:GetChildren()

for i = 1,#folderItems,1 do
	local newDummy = folder:FindFirstChild("Dummy"..i):Clone()
	newDummy.Parent = game.Workspace.NPCs
	wait(.01)
	local RandomFriendsId = game.Players:GetUserIdFromNameAsync(usernames[math.random(1,#usernames)]) 
	newDummy.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(RandomFriendsId))
end

local folder = game.Workspace.NPCs

for i,v in pairs(folder:GetDescendants()) do
	if v:IsA("Part") then
		v.Transparency = 1
		if v.Name == "Head"  then
			local items = v:GetChildren()
			for i = 1,#items,1 do
				if items[i]:IsA("Decal") then
					items[i].Transparency = 1
				end
			end
		end
	end
end

I was wondering, if the user has no friends, how can I make the dummies apply the humanoid description as the userid of 96356063?

1 Like

Write a condition that checks if the usernames table has more than one member. If it does then pick a random item from that table otherwise default to the specified UserId. You can do this quick and dirty through fake ternary or properly with an if statement that only changes the friendUserId if the username table is worth a non-zero amount of names. Having a default lets you not write an else statement.

-- Quick and dirty
local RandomFriendsId = #usernames > 0 and game:GetService("Players"):GetUserIdFromNameAsync(usernames[math.random(1, #usernames)]) or 96356063

-- With an if statement
for i = 1, #folderItems, 1 do
    local dummy = folder:FindFirstChild("Dummy" .. i)
    if not dummy then continue end

    local newDummy = dummy:Clone()
    local friendUserId = 96356063 -- Defaults to given UserId
    if #usernames > 0 then -- Evaluates and completes only if there's >0 friends
        local name = usernames[math.random(1, #usernames)]
        friendUserId = game:GetService("Players"):GetUserIdFromNameAsync(name)
    end
    newDummy.Humanoid:ApplyDescription(game:GetService("Players"):GetHumanoidDescriptionFromUserId(friendUserId))
    newDummy.Parent = workspace.NPCs
end
1 Like

Alright thank you… Once I pasted the code due to multiple variables missing I added these lines of code in various places.

local folder = game.ReplicatedStorage:WaitForChild(“NPC”)
local folderItems = folder:GetChildren()
for i = 1,#folderItems,1 do

Ending up with 0 syntax errors, and missing variables.

local folder = game.ReplicatedStorage:WaitForChild("NPC")
local usernames = {} 

local RandomFriendsId = #usernames > 0 and game:GetService("Players"):GetUserIdFromNameAsync(usernames[math.random(1, #usernames)]) or 96356063

local folderItems = folder:GetChildren()

for i = 1,#folderItems,1 do
	local dummy = folder:FindFirstChild("Dummy" .. i)
	if not dummy then continue end

	local newDummy = dummy:Clone()
	local friendUserId = 96356063 
	if #usernames > 0 then 
		local name = usernames[math.random(1, #usernames)]
		friendUserId = game:GetService("Players"):GetUserIdFromNameAsync(name)
	end
	newDummy.Humanoid:ApplyDescription(game:GetService("Players"):GetHumanoidDescriptionFromUserId(friendUserId))
	newDummy.Parent = workspace.NPCs
end

Receiving this error,

image

Seems that error is a result of applying the HumanoidDescription before parenting the character. If you reverse those lines you should be good. Did a quick five second search for that exact error and got a thread that outlined the problem (please do try to debug before reporting back issues):

2 Likes

Hey! Thanks for responding I’ll check it out. Also yeah I did look it up on my browser but that didn’t pop up, thanks.

Hey! Thanks for responding I’ll check it out. Also yeah I did look it up on my browser but that didn’t pop up, thanks.

I swapped the lines and this happened, yet I have 15 friends not 0. :thinking:

I created a repro with essentially the same code defaulting to my UserId and I didn’t receive the same issue of the script defaulting to my own UserId. You make any changes or anything? I hope you didn’t raw copy and paste my code, those were meant to be two different ways of fixing your original problem. The first one changes your check to fake ternary, the second one replaces your for loop. It’s not meant to be a whole code sample, nor joined together.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local FRIEND_NAMES = {}

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

for item, _ in iterPageItems(Players:GetFriendsAsync(LocalPlayer.UserId)) do
	table.insert(FRIEND_NAMES, item.Id)
end

for i = 1, 20 do
	local friendUserId = 6809102
	if #FRIEND_NAMES > 0 then
		friendUserId = FRIEND_NAMES[math.random(#FRIEND_NAMES)]
	end
	print(friendUserId)
end

EDIT: Just read the documentation and noticed GetFriendsAsync page elements contain a UserId, so changed the code in-post to directly use the id instead of using GetUserIdFromNameAsync. Also added more context - my code is never meant to be just copied and pasted.

2 Likes

Thanks for all your help, sorry for retracting my posts I was using my phone to reply and laptop on studio since it’s lagging like hell. I accidently duplicated the scripts and things happened, thanks again.

Hello, sorry for bumping this thread but I noticed @colbert2677 is still active on the DevForum. I know this is old but I’m a relatively new developer & I wanted to do something very similar to Roman. Is there any reason why I couldn’t do it like this?

local friendPage = plrs:GetFriendsAsync(plr.UserId):GetCurrentPage()

if #friendPage > 0 then
	print("User has friends.")
else
	print("User has no friends. How sad! :[")
end
1 Like

That does work for the use case of checking if the player has no friends at all, but if you read through the thread you’ll recognise that they do more with the friends list than just checking how populated it is. OP dresses up NPCs according to the population of a player’s friends list but if they have no friends then they wanted to pick out a default UserId to be used instead.

In other words: it works but it’s not in the scope of the question.

4 Likes