Destroy every thing except "randomPick"

local Model = script.Parent

local Objects = {
	Model.Shelf,
	Model.Table1,
	Model.Table2,
	Model.Table3,
	Model.Desk1,
	Model.Desk2,
	Model.Desk3
}

function RandomPick()
	local randomPick = Objects[math.random(1, #Objects)]
	for i, v in pairs(Model:GetChildren()) do
		
		v:Destroy()
		
	end
end	

When the script gets to v:Destroy() I want it to destroy everything except randomPick

Any help is appreciated!

Just check if v is not the random chosen object:

if not v == randomPick then
v:destroy()
end
1 Like
local Model = script.Parent

local Objects = {
	Model.Shelf,
	Model.Table1,
	Model.Table2,
	Model.Table3,
	Model.Desk1,
	Model.Desk2,
	Model.Desk3
}

function RandomPick()
	local randomPick = Objects[math.random(1,#Objects)]
	for _,object in Model:GetChildren() do
		if object ~= randomPick then
			object:Destroy()
		end
	end
end

But are all objects within the model things you want to destroy, or only the selected? Otherwise, you can save on an array:

local randomPick = Model:GetChildren()[math.random(1,#Objects)]

If the above works fine, then you could be even more efficient and do the following:

local model = script.Parent

function RandomPick()
	local objects = model:GetChildren()
	local random = math.random(1,#objects)
	for i,object in objects do
		if i == random then
			object:Destroy()
		end
	end
	objects = nil
end
1 Like

Wouldn’t that just mean that if the odd chance that v IS equal to randomPick then nothing would be destroyed.

What I was looking for, that on any circumstance randomPick would be the only thing NOT destroyed.

I’m not sure what you mean, but:

not means the opposite of a statement. So if v == randomPick is true, not v == randomPick will equal false. Although at this point, you should just use v ~= randomPick.

1 Like

That is true, but when v IS NOT equal to randomPick the script would run, but when v IS equal to it wont run, which wouldn’t work with what I’m trying to do.

Let me try and clarify. I want the math.random to choose between 7 objects.
The object that it chose is stated as randomPick, when it chooses that object, I want the script to destroy the 6 other objects, and not the randomPick

Hopefully that clears things up.

Both me and @schaap5347’s code will work as intended. Their code will only destroy v is it is not equal to randomPick. Mine does the same, but with ~= instead of not == (which would result in the exact same thing).

1 Like

UGH, I feel so stupid, I just forgot to put RandomPick() at the end so the function could actually connect.

Thank you for your help though! I appreciated it.

My script if any one wants to achieve the same thing:

local Model = script.Parent

local Objects = {
	Model.Shelf,
	Model.Table1,
	Model.Table2,
	Model.Table3,
	Model.Desk1,
	Model.Desk2,
	Model.Desk3
}

function RandomPick()
	local randomPick = Objects[math.random(1, #Objects)]
	for i, v in pairs(Model:GetChildren()) do

		if v ~= randomPick then
			v:destroy()
		else
			repeat wait() until v ~= randomPick
		end

	end
end	

RandomPick()
1 Like

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