Scan folder function not working properly

Hi there

I am currently trying to save a folder with all the models and values using this function:

function Scan(Fold)
	local function ScanFolder(Folder, Array)
		for _,Child in pairs(Folder:GetChildren()) do
			if Child:IsA("Folder") then
				Array[Child.Name] = {}
				ScanFolder(Child,Array[Child.Name])
			elseif Child:IsA("Model") then
				Array[Child.Name] = {}
				ScanFolder(Child,Array[Child.Name])
			elseif Child:IsA("ValueBase")  then
				Array[Child.Name] = Child.Value
			end
		end
		return Array
	end
	local Array = ScanFolder(Fold, {})
	return Array
end

But for some reason, when it is scanning, if there are two or more models, it will only scan the first model. I tested it with some random folder with models, but it gives the same result

It is supposed to go through all the folders and models and extract their values inside them. But it only saves on model if there is more than one in a folder.

Anyone got a fix for this?

I really appreciate all help :slight_smile:

Thanks

Might have something to do with you returning after the first check and not after the loop is done?

Oh yeah lol. Hang on lemme try something

If models have same names parented to the same object, second one will overwrite the first one.

1 Like

Ah… Ok that will be the reason

Do you have any suggestions on what I could do?

I misread the code lol.
As long as the models aren’t the same name it works prefectly fine.

You can check if your table already has an object named that and then concat a string after it like (1). Or if you want to rely on this system, just don’t have models with same names.

1 Like

The problem is that I am using it to save part of a placement system, so many of the models will have the same name. I will concatenate them with numbers i guess

If it’s a placement system you can also give them unique names from the beginning.
HttpService:GenerateGUID() is a good method for it.

1 Like

Ok thanks I am looking into it rn

So how does it work, will it never create the same random identifier?

There’s a small chance of that happening but it’s really really little and you shouldn’t take that in mind in my opinion.

1 Like

Yeah I just read that, there is an extremely small chance that it will happen, but it is miniscule. Thanks :slight_smile:

1 Like

No problem and if you want to be safe you can check if a model named that is already present on the same parent with another part. It’s as easy as

if model:FindFirstChild(generatedGUID) then generatedGUID = HttpService:GenerateGUID() end

Your script only checks for parents so chances of this happening is even more little. And if you want to be 100% safe you can do a repeat function which’ll generate a new GUID until it’s not found if you are planning for players to have massive building areas. However this really isn’t needed because chances of this happening twice is probably same with same person winning the grand prize on a lottery twice with 2 tickets.

1 Like

Thanks for the helpppppppppp :slight_smile:

1 Like

One more thing (sorry about this) But how would I retrieve the original value after the data has been loaded in? When I load in the data now, it will be something like “ItemName1298736249378492102hben1jh2jdbax” or whatever. Normally, I would use string.gsub if the extra string was set. But now I am not sure how to get the single name.

GUID’s start with { if I’m not wrong so you can just string.split it with { character. If they don’t start with it you can put the ID between {} these.