Missing players from table

so i’m trying to generate a random user ID and check if the player is valid and not deleted. if the player is deleted, i want it to choose another id until it fills up the whole table.

the for loop should put 30 usernames into the table but it ends up putting 24-29 inside it. ty!

function module:AddNPCsToQueue()
	local NPCS = {}
	
	for p = 1, #module.tilesfolder:GetChildren() do
		local randomID = math.floor(Random.new():NextNumber(2, 4_000_000_000))
		
		local success, result = pcall(function()
			return module.User:GetUserInfosByUserIdsAsync({randomID})
		end)

		if success then
			for _, userInfo in ipairs(result) do
				table.insert(NPCS, userInfo.Username)
			end
		else
			repeat task.wait()
				randomID = math.floor(Random.new():NextNumber(2, 4_000_000_000))
			until module.User:GetUserInfosByUserIdsAsync({randomID})
		end
	end

	print(NPCS)
end

image

alg solved it.

function module:AddNPCsToQueue()
	local NPCS = {}

	for p = 1, #module.tilesfolder:GetChildren() do
		local validID = false
		local randomID

		-- Repeat until a valid user ID is found
		repeat
			randomID = math.floor(Random.new():NextNumber(2, 4_500_000_000))
			local success, result = pcall(function()
				return module.User:GetUserInfosByUserIdsAsync({randomID})
			end)

			if success and result and #result > 0 then
				for _, userInfo in ipairs(result) do
					table.insert(NPCS, userInfo.Username)
				end
				validID = true  -- Valid user ID found
			end
		until validID  -- Keep looping until a valid ID is found
	end

	print(NPCS)
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.