Problems with table.insert in my script

Hello,

I am trying to Insert a Table (subs) to another Table (results)

My Issue is that I am getting duplicates of the values after I inserted the table.

I haven’t seen any solution on Devforum so I decided to make a post.

Script
local function findStringInTable(tables,str)
			local results = {}
			local subs = {}
			for _,tab in pairs(tables) do
				for i,value in pairs(tab.SubTagsFolder:GetChildren()) do
					
					if (string.match(value.Name,str)or string.match(value.Value,str)or string.match(tab.Name,str)or string.match(tab.Value,str)) and (not table.find(results[1],tab.Name)) then
						
						table.insert(results,{tab.Name}) 
						table.insert(sub,1,value.Name)
						
						for index, value2 in pairs(tab.SubTagsFolder:GetChildren())do
							if (not table.find(subs, value2.Name))then
								table.insert(subs,#subs + 1,value2.Name)
								print(unpack(subs)) --// first print
							end
						end
						table.insert(results[1], subs) --// here is where I inserted the table
					end
				end
			end
			return results
		end
		
		local results = findStringInTable(TagsDataFolder:GetChildren(),query)
		print(unpack(results[1][2])) --// second print
		print(id)
		game.ReplicatedStorage.Slide1.SearchEvent:FireClient(plr,id, results)
Output

tps fps
fps tps fps

Any help would be Useful :smiley:

2 Likes

Here is an awesome library with some super lean well done functions to help with such things. I use it almost every day.

Now your current goal: Based on the Function “Find Strings in Table” you want to output all the strings and return a table of those strings without duplicates ?

– Well depending on how you want the last result, if you are using index it means you want something like not dictionary like. So a normal table[1], table[2] kind of thing.

My suggestion is make a new table before the main loop called
local TagHolder = {}
whenever you get a tag that you insert store TagHolder[tag] = tag
now before you STORE your tag like this do a prior check
if TagHolder[tag] == nil then – After this you then add the tag insert and continue.

– THERE are more graceful ways to do this, I am just giving you a hotfix and pointing you to a helpful library

To summarize,
You are going to build a dictionary of the tags you add and check of the item in that dictionary exists already since a direct hash check is better than adding and then looping each time as you add.

Up to you though again, I want to be clear, prob not the best solution seems a touch costly of loops, BUT it will get you an easy way to nock things out.

1 Like

Sorry, I couldn’t really understand what you meant by TagHolder isn’t that not what I am already doing with the results table? If you could explain more that would be helpful.

And I went through the lume library and it probably has some useful functions but I don’t think is applicable to Roblox Studio because it is a .lua Source Code File. Unless you if can explain.

1 Like

Duplicate you say? Hmmm… Funtcion maybe run more than one time, to check it, use print("added to table") if this apears on output twice then funtcion ran 2 times.

1 Like

The first print is inside the function since it hasn’t been printed more than once then the function hasn’t been run more than once.

But I will test it again to be sure.

EDIT: Nope, Still shows the same results:
image

1 Like

Im not profesional lua coder, but if you show part of script where in table is added value, i may find problem.

This is where I define the subs table.

EDIT: I also added another value before the loop:

table.insert(sub,1,value.Name)
1 Like

for loop… well i cant find error, im not profesional lua coder, so i cant help… I will try to contact my friend that may help. Goreacraft

Well, thanks for your input anyways. :slightly_smiling_face:

1 Like

Gorea wont answer, i have second method, just wait.

edit, i searched for help at one discord server, off topic and now i wait

I somehow fixed the problem.

I didn’t realise that I was looping through the function where I defined the subs table so that was the cause of the duplication

I fixed it by using break to stop the looping

1 Like

Some error are just not visible no matter how much you look at them…