Yes, you can do that. But it’s possible for it to be nil so make sure to check if it exists.
function destroyRandom(tries)
local Part = workspace:FindFirstChild('P' .. math.random(1, 3))
if tries == nil then tries = 0 end
if Part then
Part:Destroy()
elseif tries < 10 then
destroyRandom(tries + 1)
end
end
destroyRandom()
Store a table of all the parts, no matter the names, could be thousand of parts
You select one with math.random(), then perform a loop that will delete all parts except that one that math.random is targeting.
local TableOfParts = game.Workspace:WaitForChild("FolderOfParts"):GetChildren()
local RandomPart = math.random(1, #TableOfParts)
for partID, part in pairs(TableOfParts) do
if partID ~= RandomPart then
part:Destroy()
end
end
Oh sorry, I thought you wanted to delete all parts except one, my mistake, you want to destroy a specific part only?
local TableOfParts = game.Workspace:WaitForChild("FolderOfParts"):GetChildren()
local partID = "P"..tostring(math.random(1, #TableOfParts))
game.Workspace.FolderOfParts:FindFirstChild(partID):Destroy()
tostring is useless here, Lua will automatically turn the concatenation into a String (If i remember correctly, If not, it may just be that you are adding on to the string)
Basically, there is no need to convert something that would already be converted
Would be better like this, in this case, the names of the parts are not important:
local TableOfParts = game.Workspace:WaitForChild("FolderOfParts"):GetChildren()
local randomPart = TableOfParts[math.random(1, #TableOfParts)]
randomPart:Destroy()
Yup thats true, the variable will turn into a string no matter if using tostring or not for the number. Im not used to do it like that. In a real scenario, renaming P1, P2, etc, would be very time consuming if you have hundred of parts, its better to use the table approach I sent in my last message
I think you want to create a grid of parts where every parts get slowly destroyed until no parts left.
Lets assume that you have a folder in Workspace named DestroyParts where you put all your parts and stuff you want it randomly destroyed like this:
Here is a ServerScript to slowly destroy every parts until no parts is left. Put it in ServerScriptService or Workspace.
-- Destroy a random children, returns false if no children is left
local function DestroyRandomPart(Container)
-- Create a table with all the Folder children
local Instances = Container:GetChildren()
-- Warn and cancel the execution of the function if the table is empty
if #Instances <= 0 then
warn('No Instance left in', Container)
return false
end
-- Select a random part from the table and destroy it
local RandomPart = Instances[math.random(1, #Instances)]
RandomPart:Destroy()
return true
end
print('Start game !')
while true do
local success = DestroyRandomPart(workspace.DestroyParts)
-- Stop the loop
if not success then
break
end
task.wait(1) -- Wait 1 second
end
print('Game ended !')
If you have a question, even an obvious one. I will answer to you, I’m here to help ya.
And rename 100 or more parts as P1, P2, P99, P100? … (rename them with another loop? waste of time)
Unless the names of the parts could be important, (which is not cause its possible to connect events to them, and OP is choosing a random part) then its better approach do not even care about names:
local TableOfParts = game.Workspace:WaitForChild("FolderOfParts"):GetChildren()
local randomPart = TableOfParts[math.random(1, #TableOfParts)]
randomPart:Destroy()
Distinguish the parts “if its important” would be easy, cause after applying a loop to all the parts its possible to connect an event to each of them calling a function, sending, “its name”, its position, or directly the part, in order to perform actions on it