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)
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.
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.
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.