Help with setting a PrimaryPart to every single model in the game with a code

Greetings my developers! (and non-developers)

I’m looking for a way to set a random PrimaryPart for every single Model in the game with a code since my game is full of random models everywhere, its a huge game and it might take me days to set a PrimaryPart for every single model manually, and that’s how I came with the idea of running a script on the command bar that set’s it automatically.
image
and here comes my issue, since I’m not an official scripter, I’ve attempted to do that but couldn’t figure out why my code isn’t working, and here I am once again in the form asking for your help.

local Model = game.Workspace:GetChildren()

for i=1, #Model do 
	if (Model[i].ClassName == "Model") and (Model[i].PrimaryPart == nil ) then
		
		ModleContain = Model[i]:GetDescendants()
		
		if ModleContain.ClassName == "Part" then
			Model[i].PrimaryPart = ModleContain
		end
		
	end
end

this code is the best of my amateur coding abilities, I have no idea how to make it work, and I hope you’re here to change my faith with it and help me with making it otherwise.

thank you.

local Model = game.Workspace:GetChildren()

for i=1, #Model do 
	if (Model[i].ClassName == "Model") and (Model[i].PrimaryPart == nil ) then
		
		ModleContain = Model[i]:GetDescendants()
		if #ModleContain >0 then
                 for i,v in pairs(ModleContain) do
                    if v:IsA("BasePart") then
                     Model[i].PrimaryPart = v
end 
                end
		
	end
end
end

error message
image

local Model = game.Workspace:GetChildren()

for i=1, #Model do 
	if Model[i]:IsA("Model")  then
		
		ModleContain = Model[i]:GetDescendants()
		if #ModleContain >0 then
                 for _,v in pairs(ModleContain) do
                    if v:IsA("BasePart") then
                     Model[i].PrimaryPart = v
end 
                end
		
	end
end
end

same error
image

What is “Terrain” is it even a model?

nvm saw my mistake, changed the code above.

Ummm, pardon me but, which one did you change?

local Model = game.Workspace:GetChildren()

for i=1, #Model do 
	if Model[i]:IsA("Model")  then
		
		ModleContain = Model[i]:GetDescendants()
		if #ModleContain >0 then
                 for _,v in pairs(ModleContain) do
                    if v:IsA("BasePart") then
                     Model[i].PrimaryPart = v
end 
                end
		
	end
end
end
1 Like

Here’s a more simplified version:

-- If you have models that are descendants of workspace, use workspace:GetDescendants() instead
for _, model in ipairs(workspace:GetChildren()) do
     if model:IsA("Model") then
          local primaryPart = model:FindFirstChildWhichIsA("BasePart", true) -- The true indicates to check recursively, ie; check children and their children, then the grandchildren. and their descendants etc.
          if primaryPart then
               model.PrimaryPart = primaryPart
          end
     end
end
2 Likes

Works flawlessly, thx a lot man, what can I say, you’re my hero.

this bit of code works too, you’re a bit late thought, but I appreciate it a lot…really.

1 Like