How to find an object using its name

Is there a way to do this.
Lets say we have 3 parts:
P1, P2, P3

and we do

Part = "P" .. math.random(1, 3)

Instead of me doing

if Part == "P1" then
    Workspace.P1:Destroy()
elseif if Part == "P2" then
    Workspace.P2:Destroy()
else
    Workspace.P3:Destroy()
end

Is it possible for me to just do
Workspace:FindFirstChild(“P” … math.random(1,3))

Part.Name
Also, you can simplify this code:



if Part.Name == "P1" or Part.Name == "P2" then
    Part:Destroy()
else
    Workspace.P3:Destroy()
end

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

This will error because Part is a string.

Then do this:

workspace:FindFirstChild(Part):Destroy()
1 Like

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
1 Like

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

1 Like

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

1 Like

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:
image

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.

can you not simply do:

Part = "P" .. math.random(1, 3)

workspace[Part]:Destroy()

??

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

I was assuming he just needed 3 and was trying to do it the quickest way possible.

Either way, he is going to need some way to distinguish the parts if it matters, depending on how he has it set up.

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

local Part = "P" ..tostring(math.random(1,3))

workspace[Part]:Destroy()

As said before, thats an approach, a very not efficient one with hardcoded names…

His question was this so she pretty much answered it.

workspace[Part] is exactly like workspace:FindFirstChild(Part) and workspace:WaitForChild(Part)

OP answered the question by OPself, I was just saying thats a very bad approach, based on names, and not events and a table