How would I go abouut destroying the parts that are hidden?

Hello! I’m not sure how I would go about destroying the parts where its transparency are 1. I want it to happen after it selects the randomize parts to set the transparency to 0. Sorry if there are not much information since it’s pretty straight forward. Thanks for your help!

local CollectObject = workspace:WaitForChild("XmasEvent"):WaitForChild("Stars")

local table_InfoParts = {}
for i, v in next, CollectObject:GetChildren() do
	table.insert(table_InfoParts, v)
end

function randomizeTable(table)
	if not type(table) == "table" then return end
	for i = #table, 2, -1 do
		local j = math.random(i)
		table[i], table[j] = table[j], table[i]
	end
	return table
end

randomizeTable(table_InfoParts)

for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		v.Transparency = 1
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = false
			end
	end
end

local iteration = 0
for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		v.Transparency = 0
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = true
		end
		end
	iteration += 1
	if iteration >= 6 then return end
end

I’ve tried something like this but it don’t seem to destroy the parts.

local CollectObject = workspace:WaitForChild("XmasEvent"):WaitForChild("Stars")

local table_InfoParts = {}
for i, v in next, CollectObject:GetChildren() do
	table.insert(table_InfoParts, v)
end

function randomizeTable(table)
	if not type(table) == "table" then return end
	for i = #table, 2, -1 do
		local j = math.random(i)
		table[i], table[j] = table[j], table[i]
	end
	return table
end

randomizeTable(table_InfoParts)

for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		v.Transparency = 1
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = false
			end
	end
end

local iteration = 0
for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		v.Transparency = 0
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = true
		end
				if v.Transparency == 1 then
			v:Destroy()
		end
		end
	iteration += 1
	if iteration >= 6 then return end
end

To destroy the parts with a transparency of 1 in this code, you could add a check to verify the transparency of each part before destroying it. Here is one way you could do this:

local CollectObject = workspace:WaitForChild("XmasEvent"):WaitForChild("Stars")

local table_InfoParts = {}
for i, v in next, CollectObject:GetChildren() do
	table.insert(table_InfoParts, v)
end

function randomizeTable(table)
	if type(table) ~= "table" then 
		return 
	end
	for i = #table, 2, -1 do
		local j = math.random(i)
		table[i], table[j] = table[j], table[i]
	end
	return table
end

randomizeTable(table_InfoParts)

for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		if v.Transparency == 1 then
			v:Destroy()
		end
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = false
		end
	end
end

local iteration = 0
for i, v in next, table_InfoParts do
	for i1, v1 in next, v:GetDescendants() do
		v.Transparency = 0
		if pcall(function() local A = v1.Enabled end) then
			v1.Enabled = true
		end
	end
	iteration += 1
	if iteration >= 6 then return end
end

In the code above, we added a check to verify if the ‘Transparency’ property of a part is equal to 1. If it is, we call the ‘Destroy’ method on the part to destroy it. Keep in mind that this will only work if the ‘Transparency’ property of the parts is set to 1 before this code is run.

Hi! Thanks for responding, but because it selects random parts inside a folder for it to appear, I want the destruction to happen in the script as well.

I’d recommend updating you variable names to be more readable:

for _, v in pairs(table_InfoParts) do -- You never used i, changing it to _ indicates you won't use it
	for _, v1 in pairs(v:GetDescendants()) do
		v.Transparency = 1 -- Why are you changing this for every descendant?
		if pcall(function() local A = v1.Enabled end) then -- You can probably do a :IsA() to decide if you can do this or not
			v1.Enabled = false
		end
	end
end

How would I go about changing each descendants?

Although the problem I have is trying to destroy that parts that are hidden. Do you know why using destroy won’t work?

Maybe you can do another script that says

while true do 
task.wait()
for _,v in pairs(workspace.XmaseventstarsFolder:GetChildren()) do
if v.Transparency == 1 then
v:Destroy()

--wrote this all on devforum so lmk if it doesnt work

It sets most of the stars’ transparency to 1 and it still didn’t destroy the hidden stars

Let’s say you have this hierarchy:

v1
 - child1
   - gChild
 - child2
  - gChild2
   - ggChild
v2

where v1 and v2 are in table_InfoParts.

For every descendant (v1: child1, gChild, child2, gChild2, ggChild) you are setting the part v's transparency to 1 (in this case 5 times). There is no need to do it more than once, and doing it for every descendant is a waste of time. If you look through your code, you will see that you are trying to do the same thing later on with your iteration = 0 loop. Another bi-effect of doing this means that you will only go into that loop setting the transparency if it has children (since :GetDescendants() will return an empty table if it has no descendants)

I haven’t read through the code properly to be honest, but the structure so far seems overly complicated.

Can you describe what you are trying to achieve from this script in words?

So what you are trying to say is I should delete this part above?

No. I’m saying I don’t understand why you are doing this to achieve delete a hidden star. You wrote it, which - presumably- means there is a reason for it to be there.

You’re right! I think I got it, thank you so much!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.