Is there a way to merge these two functions together?

Is there a way to merge these two functions together?

					for i,v in pairs(model) do
						v:Remove()
					end
					for i,v in pairs(model2) do
						v:Remove()
					end

Yes, if you write a method to combine two dictionaries together:

function joinDictionaries(dict1, dict2)
	local newDict = {}
	for k,v in pairs(dict1) do
		newDict[k] = v
	end
	for k,v in pairs(dict2) do
		newDict[k] = v
	end
	return newDict
end

for i,v in pairs(joinDictionaries(model, model2)) do
	v:Remove()
end

Ah, I see, but is there any way to prevent using 2 of the 3 loop functions and only needing to use 1 loop function? Or does the system don’t work that way?

1 Like

I think it’d be shorter to loop through both like that. If you have more than like 3 lines of code in each loop then I suggest you make a function for those things. You shouldn’t probably join the tables as it will give you more lines of code.

I see. Though what if I have 100 models in the dictionary? would the current method be more shorter and reliable?

If you want the model tables to be structured that way, no.
If you only do this once, just do the 2 for loops, however if you will use more models in more places in your code, writing your custom function to join tables is better.
Can you maybe give a better explanation of your use case? How many model dictionaries do you have, and how are they structured?

Would it be something like this?

local getmodel = {
model1;
model2;
model3}

local findmodel = game.Workspace:FindFirstDescendant(getmodel)

for i,v in pairs (findmodel) do
v:Remove()
end

Sorry, I’m still learning about coding.

If you put all the models inside a table, you can use the following:

local getmodel = {
    model1,
    model2,
    model3,
    model4,
    -- You can add as many model names as you want here. Just make sure the names are unique.
}
local models = {}
for i, modelName in ipairs(getmodel) do
    models[i] = game.Workspace:FindFirstChild(modelName, true)
end

--Now we can iterate through all these models:
for i, model in ipairs(models) do
    model:Remove()
end

Is this what you are looking for?

It was an example, but yes. This will help me get a better understanding and familiarize with tables.

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