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