There are 3 parts in this folder, 1 part I would like for it to maintain it’s CanCollide option. However, I’d like to select that by name if possible, not by index number.
This is a tree, it has a Stump, Base & Leaves and I would like for the Leaves to maintain the CanCollide.
I have tried an if statement, and it works, however, causes an error in the output that I would like not to happen if possible. The reason it happens is because some of my trees don’t have leaves and some do, so if I use this script across multiple trees and some don’t have leaves, this if statement shoots that error in the output.
I have also tried :FindFirstChild(“Leaves”) and this similarly causes an error of infinite yield that I would also not like to appear in my output.
local resource = game.workspace.folder
for i = 1, #resource do
resource[i].Transparency = 1
resource[i].CanCollide = false
end
task.wait(1)
for i = 1, #resource do
resource[i].Transparency = 0
resource[i].CanCollide = true
end
end
For the sake of this post, I’ve only sectioned out the part of the script I’m having trouble with.
local resource = game.Workspace.folder
for i = 1, #resource do
resource[i].Transparency = 1
resource[i].CanCollide = false
end
task.wait(1)
local leaves = resource:FindFirstChild("Leaves")
if leaves then
leaves.Transparency = 0
leaves.CanCollide = true
end
By using FindFirstChild, you can check if the “Leaves” part exists before attempting to modify its properties
I’m going to take the liberty to re-organize your example code a little.
-- I'm assuming you want to go over the children of the folder.
local resource = game.workspace.folder:GetChildren()
for index, part in resource do
if part:IsA("BasePart") then
part.Transparency = 1
part.CanCollide = false
end
end
task.wait(1)
for index, part in resource do
if part:IsA("BasePart") then
part.Transparency = 0
-- Could also just do part.CanCollide = part.Name ~= "Leaves"
if part.Name ~= "Leaves" then
part.CanCollide = true
end
end
end
With this you firstly check that only the child element that are parts are being affected. Secondly you check the name Leaves.
Thank you for all of the quick replies, however the “game.Workspace.folder” is just a placeholder and not in the actual script. I have tried the :FindFirstChild(“Leaves”) But when the Leaves part is not there it’s causing the output error. The reason I don’t make a separate script for the trees without leaves is because I have an OS script that references the day of the year UTC and deletes all of the leaves during specific times of the year. So when a player chops down this tree, this error will appear in the output. It really isn’t a huge issue, only in studio, I was just wondering if there was a way for it to check for this part by name, and if it doesn’t have it, then it wont cause an error, just end the script.