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