How to detect if a table contains the same value twice

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Remove the duplicate values

  2. What is the issue? Include screenshots / videos if possible!
    There is more that one of the same values within the table
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried looking on the forum.
    – Product module

local Product = {}
local http = game:GetService("HttpService")	

function Product.getUserAssetsRecursive(AssetId, userId, assets, pageNumber, lastLength)
	local baseUrl = "https://www.roproxy.com/users/inventory/list-json?assetTypeId="..AssetId.."&cursor=&itemsPerPage=100&pageNumber=%s&userId=%s"
	assets = assets or {}
	pageNumber = pageNumber or 1
	lastLength = lastLength or math.huge

	local requestUrl = baseUrl:format(pageNumber, userId)
	local success, result = pcall(function()
		return http:GetAsync(requestUrl)
	end)

	if success then
		if result then
			local success2, result2 = pcall(function()
				return http:JSONDecode(result)
			end)

			if success2 then
				if result2 then
					for _, asset in ipairs(result2.Data.Items) do
						if asset.Creator.Id == userId then
							if not table.find(asset,asset.Item.AssetId) then
								table.insert(assets, asset.Item.AssetId)
							end
						end
					end

					if result:len() ~= lastLength then
						lastLength = result:len()
						pageNumber += 1
						Product.getUserAssetsRecursive(AssetId,userId, assets, pageNumber, lastLength)
					end
				end
			end
		end
	end

	return assets
end

function Product.GetUserCreatedAssets(UserId : number,Assets)

	local createdAssets = {}

	for _,enum in pairs(Assets) do
		local info = Product.getUserAssetsRecursive(enum.Value,UserId)
		createdAssets[enum.Name] = info
	end
	return createdAssets
end

return Product

– Server | boothloader

local ReplicatedStorage = game:GetService(`ReplicatedStorage`)
local Modules = ReplicatedStorage.Modules

local Product = require(Modules.GetProductInfo)
local MarketPlaceService = game:GetService(`MarketplaceService`)
local Players = game:GetService(`Players`)

local AssetsWanted = {Enum.AssetType.GamePass,Enum.AssetType.Shirt,Enum.AssetType.Pants,Enum.AssetType.TShirt}

function PromptPurchase(player,productId,ProductType)
	if ProductType == `Gamepass` then
		MarketPlaceService:PromptGamePassPurchase(player,productId)
	elseif ProductType == `Asset` then
		MarketPlaceService:PromptPurchase(player,productId)
	end
end

function ListUserProducts(player : Player,Stand)
	local data = Product.GetUserCreatedAssets(2233944403,AssetsWanted)

	print(data)
	
	for _, gamepass in data[`GamePass`] do
		local ProductInfo = MarketPlaceService:GetProductInfo(gamepass,Enum.InfoType.GamePass)
		if not ProductInfo.PriceInRobux then continue end

		local template = Stand.SignPrices.UI.ScrollingFrame.Template:Clone()
		template.Name = gamepass
		template.Text =  `$ {ProductInfo.PriceInRobux}`
		template.Parent = Stand.SignPrices.UI.ScrollingFrame
		template.Visible = true
		template:SetAttribute(`ProductId`,gamepass)
		
		template.MouseButton1Click:Connect(function()
			PromptPurchase(player,gamepass,`Gamepass`)
		end)
	end

	for _, gamepass in data[`Pants`],data[`Shirt`], data[`TShirt`] do

		local ProductInfo = MarketPlaceService:GetProductInfo(gamepass,Enum.InfoType.Asset)
		if not ProductInfo.PriceInRobux then continue end
		
		local template = Stand.SignPrices.UI.ScrollingFrame.Template:Clone()

		template.Name = gamepass
		template.Text =  `$ {ProductInfo.PriceInRobux}`
		template.Parent = Stand.SignPrices.UI.ScrollingFrame
		template.Visible = true
		template:SetAttribute(`ProductId`,gamepass)

		template.MouseButton1Click:Connect(function()
			PromptPurchase(player,gamepass,`Asset`)
		end)
	end
end

for _,proxy in pairs(workspace.Stands:GetDescendants()) do
	if proxy:IsA(`ProximityPrompt`) then
		proxy.Triggered:Connect(function(player)
			if player:GetAttribute(`HasBooth`) then return end
			proxy.Enabled = false
			player:SetAttribute(`HasBooth`,true)
			proxy.Parent.Parent.Values.Owner.Value = player
			local Info = proxy.Parent.Parent.SignDescription.SurfaceGui.Frame

			Info.OwnerLabel.Text = player.Name

			ListUserProducts(player,proxy.Parent.Parent)
		end)
	end
end
local yourtable = {thing1, thing2, thing1, thing1, thing2, thing3} -- your table from which you want the things removed
local onlysingle = {} -- table which will contain only the single instances of each thing

for i, v in yourtable do
	if table.find(onlysingle, v) then continue end
	table.insert(onlysingle, v)
end
-- onlysingle == {thing1, thing2, thing3}
1 Like

That did not work, here is what it printed (well warned-)
image

Would this post help you?

or

1 Like

I have looked at that post but it seems that code deletes the duplicate and original

Would it not be

for i, v in yourtable do
	if table.find(onlysingle, v) == nil then 
       table.insert(onlysingle, v)
 end
end

?

1 Like

I just tried that and it did not change anything.

Set the yourtable variable to t.GamePass instead of t. The code the user provided isn’t recursive.

1 Like

Ah that is a good point I dont know how I didnt notice that.

if not table.find(asset,asset.Item.AssetId) then
      table.insert(assets, asset.Item.AssetId)
end

You use the word ‘asset’ and then ‘assets’ in the second part, could this be causing problems?

1 Like

Ill check that in a second, for now:

AFK

That was the issue, thank you so much for your help!

1 Like

Of course! Glad you could get it fixed :slight_smile:

1 Like

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