Removing a table of a player's friends when the player leaves

Hello all,

I was messing around with making an NPC’s HumanoidDescription a random active players friend’s HumanoidDescription. I got it mostly set up but then I wanted to remove the players friends from the table (I had set up this table to get the humanoid description of the friend) when the random active player leaves. So the question is, how do remove a specific players friends from the “usernameOfFriends” table when the player leaves?

I set up 2 tables: 1 table for the “active players” in the game and 1 table for the usernames of the friends (of the players)

If you need me to clarify more, please ask.

Code:

local Players = game:GetService("Players")

local ActivePlayers = {}

local RandomPlayerUserName = tostring(ActivePlayers[math.random(1, #ActivePlayers)])

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 userId = Players:GetUserIdFromNameAsync(RandomPlayerUserName)

local friendPages = Players:GetFriendsAsync(userId)

local usernamesofPlayersFriends = {}
for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernamesofPlayersFriends, item.Username)
end


local randomPlayersFriends = usernamesofPlayersFriends[math.random(1, #usernamesofPlayersFriends)]

local friendsUserId = Players:GetUserIdFromNameAsync(randomPlayersFriends)

local OutfitDescription = Players:GetHumanoidDescriptionFromUserId(friendsUserId)

--script.Parent.Humanoid:ApplyDescription(OutfitDescription)



game.Players.PlayerAdded:Connect(function(player)
	table.insert(ActivePlayers, player)
end)

game.Players.PlayerRemoving:Connect(function(player)
	table.remove(ActivePlayers, ActivePlayers[player])
	--add something here to remove the friends from the table right?
end)`

First declare at top of script local usernamesofPlayersFriends = {}
then I think what you could do is set usernamesofPlayersFriends[Player.Name] = {} in the playeradded function – this sets up a table using the playername as the main key then set the other usernames as values in this table with insert
then use table.insert(usernamesofPlayersFriends[Player.Name], item.Username) to add names to the table
then in your PlayerRemoving function usernamesofPlayersFriends[Player.Name] = nil which will clear all of the stored usernames for this player