How to unanchor a random part in a model

Hello!

I’m trying to make a script where it would choose it’ll delete a random part in a model, like it’s disappearing bit by bit. The issue is I don’t know how to go about it, and couldn’t find any solution.

Any help is appreciated!

local Model -- Path to your model

local function DeleteRandomPart()
	local ChildrenOfModel = Model:GetChildren()
	local PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)]
	PartToDelete:Destroy()
end

Pretty sure this would work, you do just have to call the DeleteRandomPart function

2 Likes

Do I have to call it multiple times in a loop? I want to make it so the whole model is gone.

This function only deletes 1 part, meaning if you want to delete more than one, you do have to use a loop

1 Like

You can do a for loop for the number of children in the model.

local Model -- Path to your model

local function DeleteRandomPart()
	local ChildrenOfModel = Model:GetChildren() --gets the array of the model's children
	local PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)] --retrieves a random child of the model
	PartToDelete:Destroy() --destroys the randompart
end

for count = 1, #Model:GetChildren() do --repeats the line below for the number of children in the model
DeleteRandomPart() --calls the function
end

Courtesy to @Corrupted_LuaFile for the actual function.

3 Likes

It works! Thank you so much! :grinning:

1 Like