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

How do I constantly delete objects that are not the children of a certain object?

Asked by 7 years ago

I have created a folder and placed the important parts of my game inside of it, and now I want to endlessly delete any objects that are not within the folder

Here is the script:

while true do
    local workchild = game.Workspace:GetChildren()
    for i = 1, #workchild do
        if workchild[i].ClassName ~= "Folder" or "Terrain" then
            workchild[i]:Destroy()
        end
    end
    wait(0.01)
end

The problem here is that the if statement gets ignored and the script is attempts to destroy the terrain - which is something it cannot do.- This causes an error, the script stops, and i'm sure this isn't the only problem with the script.

0
I just tested it and at each test the if statement got NOT ignored. Could you explain your problem in more detail? Like does it get ignored in 1 of the 5 tests? Or where do you have your script? DeepDev 101 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Hey SparklingFruitPunch,

So your code's issues that I've picked up on are very simple. The 2 issues you have is you're using an or statement and you are not checking for the ClassName to be 'Terrain', you just check if "Terrain" is "Terrain". I know this might seem stupid/confusing at first but, I'll explain each issue in detail with an example at the end.

Your Or Statement

So, your code logic is just messed up in your if statement. With your or statement if the first statement is true then it will just go on with the rest of the code. That's why it tries to delete 'Terrain' 'cause it's ClassName isn't 'Folder'. Below is a personal example.

Personal Example A

local x = true; -- Variable declaration for a boolean with the value 'true'.
local y = false; -- Variable declaration for a boolean with the value 'false'.

if x or y then -- First checks if 'x' is true and if it is then it moves on and doesn't even look at the second part however, if 'x' is false then it will move on to the next part of the if statement to check if 'y' is false or true and if it's true then it will pass the if statement. 
    print("X is true.");
end

Anyway, what you were having trouble with there was a simple aspect of programming commonly addressed as programming logic but, professionally called "De Morgan's Law". Check it out if you want, it's quiet useful to learn about.

Now, moving on to your next issue.

Your Checking For Terrain

Now in your if statement when you check for "Terrain" you aren't checking if the classname is 'Terrain'. Computers are dumb alright so you can't really use normal language to talk to them. Your if statement was just corrupted in the way that it didn't restate to check if the classname is "Terrain". In your if statement's second bit you just check if the string "Terrain" is "Terrain". I know it might sound confusing but, it's not. Below is a personal example of what I mean.

Personal Example B

local str = "Terrain"; -- Variable declaration for the string "Terrain" 

if str then -- Checks if str is not nil.
    print("The string is 'Terrain'"); 
end

So, to simply fix what you need to get fixed is to restate the first part except, change what you are checking for. Rather then checking for folder this time though check for terrain in the second part. Since, you've already checked for folder. Below is a personal example.

Personal Example C

local folder, terrain = "Folder", "Terrain";
local parts = workspace:GetChildren();

for i = 1, #parts do
    if parts[i].ClassName ~= folder and parts[i].ClassName ~= terrain then
        print("It's not a folder and it's not terrain.");
    end
end

As you can see above I restated to check if the classname is 'Terrain'. You have to make sure that you restate that because computers don't understand if you mean either of the options which is why you must restate the property you are checking.

Well, I hope I helped in one way or another and have a nice day/night.

~~ KingLoneCat

0
I tried out Personal Example C and although there were no errors, the folder ended up getting destroyed. I would also like to state that the print function ran fine. SparkingFruitPunch 4 — 7y
0
Are you sure that the object that has all the 'important stuff' has a class name of "Folder" or is it's classname something else like "Configuration" KingLoneCat 2642 — 7y
0
Ok, nvm. I am gonna edit the answer and it'll be fine now. KingLoneCat 2642 — 7y
Ad

Answer this question