Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problem with searching though the Workspace (IsA:("Part"))?

Asked by
Mystdar 352 Moderation Voter
9 years ago

I am trying to remove all parts of a certain colour, but the script is not working. This is the script (Normal script in the workspace):

function Colour_Delete(Colour)
    for index, child in pairs(workspace:GetChildren()) do
        if child.ClassName:IsA("Part") then
            if child.BrickColor == Colour then
                child:remove()
                print (child.."Removed")
                wait(.1)
            end
        end
    end
end

Colour_Delete("Dark stone grey")

This is the output:

19:32:12.579 - Workspace.Side_Stoning:3: attempt to call method 'IsA' (a nil value) 19:32:12.580 - Stack Begin 19:32:12.581 - Script 'Workspace.Side_Stoning', Line 3 - global Colour_Delete 19:32:12.581 - Script 'Workspace.Side_Stoning', Line 13 19:32:12.582 - Stack End

2 answers

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

As your output suggests, their is a simple mistake that you made. Try this:

function Colour_Delete(Colour)
    for index, child in pairs(workspace:GetChildren()) do
        if child:IsA("Part") then --You don't need to use "ClassName"
            if child.BrickColor == Colour then
                child:remove()
                print (child.."Removed")
                wait(.1)
            end
        end
    end
end

Colour_Delete("Dark stone grey")

I think that this should work now. If not, leave a comment below, and I'll look through again to see if I missed anything. Hope I helped :P

Ad
Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

To elaborate, the IsA is a method on instances, yet you tried to call it on a property. This is why you get the error. Dyler's code should work.


However, it would also be a good idea to combine the if statements.

if child:IsA("Part") and child.BrickColor == Colour then

This may look scary to you. After all, if the child isn't a Part, then it won't have a BrickColor. However, computers are lazy and it won't bother checking the second condition if the first one is found to be false.


You may want wedges, corner wedges, and unions to also be deleted. In this case, check if they're BaseParts. All part objects are inside a higher BasePart class, so checking it that way will allow for wedges and such.

child:IsA("BasePart")
0
Thanks, however, unions and negate parts aren't under the BasePart, upvoted. Mystdar 352 — 9y
0
Really? They should be. Perci1 4988 — 9y

Answer this question