Why is it not able to find the specific child of workspace? Need help for spawn parts game

I am trying to make a spawn parts game and with this clear all button

It won’t clear as expected.
The logic i’m trying to do here is…
when the player presses “Press on me to spawn a brick” it will clone a brick called “PartSample”,
now when the player presses “Clear all spawned parts” a serverscript inside the gui of those two buttons will clear all of the children in the workspace named “PartSample”.

Now the problem im having here is that it won’t clear all of the children in the workspace named “PartSample”. It is the correct spelling too. This is the serverside script that runs for the “Clear all spawned parts” button.

	local foundPart = false

	for _, child in ipairs(workspace:GetChildren()) do
		if child:IsA("Part") and child.Name == "PartSample" then
			child:Destroy()
			foundPart = true
			break
		end
	end

	return foundPart
end

script.Parent.ClearAllPartsSpawned.MouseButton1Click:Connect(function()
	local success = game:GetService("RunService"):IsServer() and removeParts()

	if success then
		script.Parent.ClearAllPartsSpawned.Text = "Successfully cleared the needed parts!"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "ClearAllPartsSpawned"
	else
		script.Parent.ClearAllPartsSpawned.Text = "Did not find the needed parts to be cleared"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
		print("No matching part found")
	end
end)

Now as you can tell in the code block of

		script.Parent.ClearAllPartsSpawned.Text = "Successfully cleared the needed parts!"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "ClearAllPartsSpawned"
	else
		script.Parent.ClearAllPartsSpawned.Text = "Did not find the needed parts to be cleared"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
		print("No matching part found")
	end

On the else line right? It will print “No matching part found.”

With this script


	for _, child in ipairs(workspace:GetChildren()) do
		if child:IsA("Part") and child.Name == "PartSample" then
			child:Destroy()
			foundPart = true
			break
		end

You can see that it will check if the workspace’ kids have a child named “PartSample”, that is the child i’m targeting to remove.

But it will just print “No matching part found”(Obviously because of the else line in one of the previous code.)

It is the correct name.

Here is a video of the game.

Can anyone modify the codes I provided to fix it? This would help alot,
thanks.

Hi, when parts are spawned is that done on the server or client?
You can check the serverside view of the workspace to see whether it is there or not. As this would explain why it can’t find the part you are looking for.
I also noted a break in your loop, which would prevent the code from getting ALL of the parts spawned and destroying them. But also note in the current setup this would affect ALL players as it is being run on the server.

Is there already an solution on this?

Hi! It will spawn only on the client, I used a local script to handle the part cloning.

Hello, I think this might help you: based on the code, it seems like the issue might be with the way you’re checking for children with the name “PartSample” in the workspace. The code checks if the child is a “Part” and has the name “PartSample” before destroying it. However, it only destroys the first matching part it finds and then breaks out of the loop.

To clear all parts with the name “PartSample” in the workspace, you should modify your script to remove the break statement and remove the foundPart variable. Here’s an updated version of the script:

script.Parent.ClearAllPartsSpawned.MouseButton1Click:Connect(function()
	local success = game:GetService("RunService"):IsServer() and removeParts()

	if success then
		script.Parent.ClearAllPartsSpawned.Text = "Successfully cleared the needed parts!"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
	else
		script.Parent.ClearAllPartsSpawned.Text = "Did not find the needed parts to be cleared"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
		print("No matching part found")
	end
end)

function removeParts()
	local parts = workspace:GetDescendants()
	local foundParts = false

	for _, part in ipairs(parts) do
		if part:IsA("Part") and part.Name == "PartSample" then
			part:Destroy()
			foundParts = true
		end
	end

	return foundParts
end

just make sure to replace your original server-side script with this updated version and test it again to see if it clears all the parts as expected.

Sadly it didn’t work, I am not sure if its me or the script itself, this is a hierarchy on how I placed it.

Try printing game: GetService (“runservice”):isServer()
(Sorry about formatting, autocorrected)

Sorry, I meant luciant
Looks like you’re breking out of the loop after finding the 1st one and not sure why you’re using isServer().

Hey, I think I will remove that and how do you do the opposite of breaking out of the loop

Just remove the break.

If you create parts on the client and all this code is on the client, then checking whether the code is running on the server will most likely return false or nil.

Like this?


for _, child in ipairs(workspace:GetChildren()) do
	if child:IsA("Part") and child.Name == "PartSample" then
		child:Destroy()
		foundPart = true
	end
end



script.Parent.ClearAllPartsSpawned.MouseButton1Click:Connect(function()
	local success = game:GetService("RunService"):IsServer() 

	if success then
		script.Parent.ClearAllPartsSpawned.Text = "Successfully cleared the needed parts!"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "ClearAllPartsSpawned"
	else
		script.Parent.ClearAllPartsSpawned.Text = "Did not find the needed parts to be cleared"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
		print("No matching part found")
	end
end)

Yes but change the line
local success = game...etc
to
local success = foundPart

Oh and also add to the last line of this function
foundPart = false
To reset the variable for next time

Didn’t work :c

Heres a video:

It’s 11pm here in the Philippines I will go to sleep afterwards so I will try to reply you back ASAP

Heres a copy of my game feel free to scan it if you don’t trust it <3
COPYFORSPAWN.rbxl (39.9 KB)

Just send me back the edited file and I will test it ty!

Hello there, why don’t you try and print every child that is in workspace?
This way you can know if your ‘if statement’ isn’t working or if the part even exists.

EDIT: I downloaded the file, and inmediately realised the for loop only fires at the beginning and never again, i made the function for the button and it just worked.
Script

script.Parent.ClearAllPartsSpawned.MouseButton1Down:Connect(function()
	for _, child in ipairs(workspace:GetChildren()) do
		if child:IsA("Part") and child.Name == "PartSample" then
			child:Destroy()
			foundPart = true
		end
	end
end)

Didn’t realise you had removed the function wrapper.

local function removeParts()
local foundPart = false
for _, child in ipairs(workspace:GetChildren()) do
	if child:IsA("Part") and child.Name == "PartSample" then
		child:Destroy()
		foundPart = true
	end
end
return foundPart
end


script.Parent.ClearAllPartsSpawned.MouseButton1Click:Connect(function()
        local success = removeParts()

	if success then
		script.Parent.ClearAllPartsSpawned.Text = "Successfully cleared the needed parts!"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "ClearAllPartsSpawned"
	else
		script.Parent.ClearAllPartsSpawned.Text = "Did not find the needed parts to be cleared"
		wait(2)
		script.Parent.ClearAllPartsSpawned.Text = "Clear all spawned parts"
		print("No matching part found")
	end
end)

Just don’t break the loop at all. Let it finish.

Sorry it was late at night I was not able to comprehend what I broke.