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