How to only destroy parts in a folder?

Hello! I am trying to only destroy parts that are in the folder and give the player cash in the leaderstats. This is the current script:

function trashCollect()
	local playerInBoat = false

	script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
		if script.Parent.Occupant then
			playerInBoat = true
		end
	end)

	local trashcollector = script.Parent.Parent.Boat.TrashCollectors
	trashcollector.Touched:Connect(function(hit)
		if hit:IsA("BasePart") and playerInBoat then
			hit:Destroy()
		end	
	end)
end

This is the folder located in:

game.ReplicatedStorage.Content.Trash
local folder = workspace.Folder --Example.

for _, child in ipairs(folder:GetChildren()) do
	if child:IsA("BasePart") then
		child:Destroy()
	end
end
1 Like

This is my current script updated with your code and is not working?


local playerInBoat = false
local folder = game.ReplicatedStorage.Content.Trash

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.Occupant then
		playerInBoat = true
	end
end)

local trashcollector = script.Parent.Parent.Boat.TrashCollectors
trashcollector.Touched:Connect(function(hit)
	for _, child in ipairs(folder:GetChildren()) do
		if child:IsA("BasePart") or child:IsA("MeshPart") then
			child:Destroy()
		end
	end
end)


local folder = -- Where the folder is located.
local debris = game:GetService("Debris")

for _, child in ipairs(folder:GetChildren()) do
    if child:IsA("BasePart") and child.Parent:IsA("Folder") then
        debris:AddItem(child, 0) -- Change 0 to any delay time!
    end
end

PLEASE make sure to change the folder variable to the actual folder location!

I am trying to destroy the part. What does “Debris” do? also im trying to do BasePart and MeshPart

Debris is a system where you can destroy parts.

game:GetService("Debris"):AddItem(ITEM,SECONDS)

Replace ITEM with the item you want to be destroyed.
Replace SECONDS with the seconds to wait before being destroyed

Debris is a service that destroys the object, but you can schedule the removal without yielding code. It is also recommended to use when objects have a fixed lifetime.

Also, here’s an updated piece to assist you further:

local folder = -- Where the folder is located.
local debris = game:GetService("Debris")
local waitTime = 0

for _, child in ipairs(folder:GetChildren()) do
    if child:IsA("BasePart") or child:IsA("MeshPart") then
        debris:AddItem(child, waitTime)
    end
end

I’d like to add that if you want the if statement to support more ‘types’ of objects, just use child:IsA(""), and in the quotation marks, add the ClassName of the object!

It would help to provide us what exactly isn’t working here, otherwise we’re just straight up guessing at this point

If you could also show us your Output Tab as well for debugging, that’d be useful as well :slightly_smiling_face:

local Seat = script.Parent
local TrashCollect = Seat.Parent.Boat.TrashCollectors
local Folder = game.ReplicatedStorage.Content:WaitForChild("Trash")

local function trashCollect()
    local playerInBoat = false

    Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
        if Seat.Occupant ~= nil then
            playerInBoat = true
        end
    end)

    local Connection
    local function Touched(Hit)
        Connection:Disconnect()

        for _, Child in ipairs(Folder:GetChildren()) do
            if Child:IsA("BasePart") or Child:IsA("MeshPart") then
                Child:Destroy()
            end
        end

    end

    Connection = TrashCollector.Touched:Connect(Touched)
end

Aye, who doesn’t love a good Lua guessing game? It may only take 1000 replies, but we’ll find solution at some point! :grin:

Sorry I am currently getting another error with a script which I can not test this one without ti, it basically spawns the trash thats getting destroyed but everytime i put the boat in workspace it spams me errors.

Boat Location:
image

Error:

SpawnerScript:

local function selectTrash()

return trash:GetChildren()[math.random(1, #trash:GetChildren())]

end
function trashCollect()
	local playerInBoat = false

	script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
		if script.Parent.Occupant then
			playerInBoat = true
		end
	end)

	local trashcollector = script.Parent.Parent.Boat.TrashCollectors
	trashcollector.Touched:Connect(function(hit)
		if hit:IsA("BasePart") and hit.Parent == game.ReplicatedStorage.Content.Trash and playerInBoat then
			hit:Destroy()
		end	
	end)
end

Thanks Ill check this out after I fix this issue:

show the script for spawning the boat

I didnt spawn the boat I just moved it from replicated storage to workspace and clicked play

That error means that your second argument to math.random() is less than the first. #trash:GetChildren() is likely returning 0.

Edit to this guy :

local folder = workspace.Folder --Example.

for _, child in ipairs(folder:GetDescendants()) do
	if child:IsA("BasePart") then
		child:Destroy()
	end
end

I am trying this but it just gives me this error.


image