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
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
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.
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
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).
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()