Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to random select a child, and delete it?

Trying to randomly delete all parts of a model in no particular order, by fading them out then removing them with destroy(), but the code doesn't seem to be executing and am completely blank.

house = workspace.SuburbanHome

function _G.findRandomChild(model)
return model:children()[math.random(1,#model:children())]
end

function destroyItem(part) 
	for x = 1, 10 do
		nextRandom.transParency = nextRandom.Transparency - .1;
		wait()
		if nextRandom.transParency == 0 then
			nextRandom:Destroy()
		end
	end 
end


for i =1, 10,000 do
	nextRandom = findRandomChild(house)
	destroyItem(nextRandom)
end

1 comment

0

If you want it to be invisible, you should be adding transparency instead of subtracting it (although this may not be the solution) check line 09

1 answer

There are some typos and problems with your script, but I appreciate the effort you put in.

I kind of dislike how you did this, as it is confusing for me, so I'm going to remake your whole script to make it more simple and understandable. I'll explain how it works below the code block.

##Script

local model = workspace:WaitForChild('Model')

while wait(.1) do
	local modeltable = model:GetDescendants()  -- Change to :GetChildren() if you don't want descendants destroyed.
	if #modeltable == 0 then
		break
	end
	local randompick = modeltable[math.random(#modeltable)]
	if randompick:IsA('BasePart') then
		for i = randompick.Transparency, 1, .1 do
			wait()
			randompick.Transparency = i
		end
		randompick:Destroy()
	end
end

print('Broke')

##Explanation I decided to do this in a loop. Every .1 seconds, the script gets the descendants and checks if there are any parts in the model. If there is, it'll continue, otherwise it'll break the whole loop and move on to the print. The script picks a random part through "randompick", checks if it actually a part, then changes its transparency to 1 based on the part's transparency (it'll make it look smoother now if any of your parts are already transparent). After it finishes making the part invisible completely, it'll destroy it. Then the loop makes the whole thing repeat until there are no parts left in the model.

Hope this helped!

1 comment

0

It did; the style in which I wrote the original piece is just from how I do any java programming at school, and I'm not yet able to do much as efficiently in roblox anymore

This discussion is preserved as it was. New answers, comments, and votes are closed.