How can I clean up this code? Its quite messy towards the middle

How can I clean up this code? I’m trying to make a mineable ore, but when you finish mining it, only the base stone mesh disappears. Instead of referencing each and every part on the ore, how can I reference them all at once to make my code cleaner?

code:

			script.Parent.Parent.Ores.Part1.Transparency = 1
			script.Parent.Parent.Ores.Part2.Transparency = 1
			script.Parent.Parent.Ores.Part3.Transparency = 1
			script.Parent.Parent.Ores.Part4.Transparency = 1
			script.Parent.Parent.Ores.Part5.Transparency = 1
			
			script.Parent.Parent.Ores.Part1.CanCollide = false
			script.Parent.Parent.Ores.Part2.CanCollide = false
			script.Parent.Parent.Ores.Part3.CanCollide = false
			script.Parent.Parent.Ores.Part4.CanCollide = false
			script.Parent.Parent.Ores.Part5.CanCollide = false
1 Like

You could use a loop

for index = 1, 5 do
    local orePart = script.Parent.Parent.Ores:FindFirstChild("Part" .. index)
    if orePart then
        orePart.Transparency = 1
        orePart.CanCollide = false
    end
end
4 Likes

Here is a simple for loop you can use to make your code as short as possible:

for index, orePart in pairs(script.Parent.Parent.Ores:GetChildren()) do
    if orePart:IsA("Part") then
        orePart.Transparency = 1
        orePart.CanCollide = false
    end
end

This is also useful if you want to add more parts to your ore in the future since you won’t have to change the code

2 Likes