Randomised Generation of Friend ID's has Incorrect Layout

I’m currently creating a Randomised Generation of Friend ID’s that spawn in the set of the MaxClothing Value. I’ve realised a few issues while testing the Script, the Friend ID’s are always listed the same and the Generation for the Characters aren’t applied to every Dummy (Model), there’s (4) Dummies in total.

Output:

  12:37:16.399  Friend ID : 3342697767  -  Server - Clothing:65
  12:37:16.399  Friend ID : 972824537  -  Server - Clothing:65
  12:37:16.399  Friend ID : 2664361111  -  Server - Clothing:65
  12:37:16.399  Friend ID : 4448468670  -  Server - Clothing:65
  12:37:16.399  Reached Friend ID limit, stopping further additions.  -  Server - Clothing:67
  12:37:19.317  Friend ID limit reached. No further IDs will be assigned.  -  Server - Clothing:157

ISSUE: [THE 4 FRIEND ID’S THAT ARE DISPLAYED ARE THE ONLY FRIEND ID’S THAT GENERATE.)
SOLOTUION: [IMPLEMENT A RANDOMISED FRIEND ID’S GENERATION FOR THE LAYOUT.)

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local Players = game:GetService("Players")

local Settings = require(ReplicatedStorage.ServerSettings.S_Settings)

local FriendPages = nil
local RequestSuccess = false

local ClothingLoaded = {}

local function ShuffleList(list)
	local n = #list
	for i = n, 2, -1 do
		local j = math.random(i)
		list[i], list[j] = list[j], list[i]
	end
end

local function GenerateRandomFriendID(FriendIDs)
	if #FriendIDs == 0 then
		return nil
	end
	local RandomIndex = math.random(#FriendIDs)
	return FriendIDs[RandomIndex]
end

local function IteratePages(Pages)
	return coroutine.wrap(function()
		local PageNumber = 1
		while true do
			for _, Friend in ipairs(Pages:GetCurrentPage()) do
				coroutine.yield(Friend, PageNumber)
			end
			if Pages.IsFinished then
				break
			end
			Pages:AdvanceToNextPageAsync()
			PageNumber += 1
		end
	end)
end

local function LoadFriendIDs(Player)
	local FriendIDs = Player:FindFirstChild("FriendIDs")
	if FriendIDs then
		return FriendIDs:GetChildren()
	end

	local ClothingCount = Settings.MaxClothing
	local UserID = Player.UserId
	local FriendIDs = {}

	local Success, Error = pcall(function()
		FriendPages = Players:GetFriendsAsync(UserID)
	end)

	RequestSuccess = Success

	if RequestSuccess then
		for Friend, _ in IteratePages(FriendPages) do
			if #FriendIDs < Settings.MaxClothing then
				table.insert(FriendIDs, Friend.Id)
				print("Friend ID : " .. Friend.Id)
			else
				print("Reached Friend ID limit, stopping further additions.")
				break
			end
		end

		local FriendIDsFolder = Instance.new("Folder")
		FriendIDsFolder.Name = "FriendIDs"
		FriendIDsFolder.Parent = Player

		for _, FriendID in ipairs(FriendIDs) do
			local FriendIDValue = Instance.new("StringValue")
			FriendIDValue.Name = tostring(FriendID)
			FriendIDValue.Value = tostring(FriendID)
			FriendIDValue.Parent = FriendIDsFolder
		end

		return FriendIDs
	else
		warn("Failed to fetch friends list: " .. (Error or "Unknown Error"))
		return {}
	end
end

local function LoadFriendsClothing(Player)
	local FriendIDs = LoadFriendIDs(Player)

	if #FriendIDs == 0 then
		warn("No valid Friend IDs found!")
		return
	end

	local ClothingCount = Settings.MaxClothing
	local AssignedFriendIDs = {}
	local UnusedFriendIDs = {table.unpack(FriendIDs)}
	local ClothingLoaded = {}

	ShuffleList(UnusedFriendIDs)

	repeat
		local Clothing = ServerStorage:WaitForChild("Clothes"):Clone()
		Clothing.Parent = Settings.Backup

		local DummyModels = {}
		for i = 1, Settings.MaxClothing do
			local ClothingPart = Clothing:FindFirstChild("Clothing" .. i)
			if ClothingPart then
				local Dummy = ClothingPart:FindFirstChild("Dummy" .. i)
				if Dummy then
					table.insert(DummyModels, Dummy)
				else
					warn("Dummy not found for Clothing" .. i)
				end
			else
				warn("Clothing part " .. i .. " not found!")
			end
		end

		if #DummyModels > 0 then
			if typeof(Settings.Backup) == "Instance" then
				Clothing.Parent = Settings.Backup
			else
				warn("Invalid Backup location. Ensure Settings.Backup is an Instance.")
				Clothing:Destroy()
				break
			end

			for _, Dummy in ipairs(DummyModels) do
				if Settings.MinClothing >= Settings.MaxClothing then
					print("Friend ID limit reached, stopping further assignments.")
					break
				end

				local RandomFriendID
				if #UnusedFriendIDs > 0 then
					RandomFriendID = GenerateRandomFriendID(UnusedFriendIDs)
					if RandomFriendID then
						table.remove(UnusedFriendIDs, table.find(UnusedFriendIDs, RandomFriendID))
					else
						print("No more Friend IDs available.")
						break
					end
				else
					print("No more Friend IDs available.")
					break
				end

				AssignedFriendIDs[RandomFriendID] = true
				Settings.MinClothing = Settings.MinClothing + 1

				if Settings.MinClothing >= Settings.MaxClothing then
					print("Friend ID limit reached. No further IDs will be assigned.")
					break
				end

				local ExistingFriendID = Dummy:FindFirstChild("AssignedFriendID")
				if not ExistingFriendID then
					local FriendIDValue = Instance.new("StringValue")
					FriendIDValue.Name = "AssignedFriendID"
					FriendIDValue.Value = tostring(RandomFriendID)
					FriendIDValue.Parent = Dummy
				end

				local HumanoidDescription = nil
				if RandomFriendID ~= 0 then
					local Success, Error = pcall(function()
						HumanoidDescription = Players:GetHumanoidDescriptionFromUserId(RandomFriendID)
					end)

					if Success and HumanoidDescription then
						local Humanoid = Dummy:FindFirstChild("Humanoid")
						if Humanoid then
							local ApplySuccess, ApplyError = pcall(function()
								Humanoid:ApplyDescription(HumanoidDescription)
							end)
							if not ApplySuccess then
								warn("Failed to apply HumanoidDescription for Dummy: " .. Dummy.Name .. " Error: " .. ApplyError)
							end
							local NameSuccess, NameError = pcall(function()
								Humanoid.DisplayName = Players:GetNameFromUserIdAsync(RandomFriendID)
							end)

							if not NameSuccess then
								warn("Failed to set DisplayName: " .. NameError)
							end

							Dummy.Name = tostring(RandomFriendID)

							local FriendValue = Dummy:FindFirstChild(Settings.Friend) or Instance.new("StringValue")
							FriendValue.Name = Settings.Friend
							FriendValue.Parent = Dummy
							FriendValue.Value = Player.Name

							local ClothingValue = Dummy:FindFirstChild(Settings.Clothing) or Instance.new("BoolValue")
							ClothingValue.Name = Settings.Clothing
							ClothingValue.Parent = Dummy
							ClothingValue.Value = true

							ClothingLoaded[RandomFriendID] = true
						else
							warn("Humanoid not found for Dummy: " .. Dummy.Name)
						end
					else
						warn("Failed to apply HumanoidDescription for Friend ID: " .. RandomFriendID)
						Clothing:Destroy()
					end
				else
					warn("Invalid Friend ID (0) assigned to Dummy: " .. Dummy.Name)
				end
			end

			ClothingCount = ClothingCount - 1
		else
			warn("No valid Dummy models found for clothing!")
			Clothing:Destroy()
		end
	until ClothingCount <= 0 or Settings.MinClothing >= Settings.MaxClothing
end

Players.PlayerAdded:Connect(LoadFriendsClothing)

ISSUE: [DOESN’T APPLY THE CHARACTER TO EVERY DUMMY (MODEL) AND THERE’S (4) DUMMIES IN TOTAL.)
SOLOTION: [FIX THE REPETITVE FRIEND ID’S GENERATION FOR THE DUMMIES AND RANDOMISE THE FRIEND ID’S.]

Video:

I will respond ASAP.

Max characters