Removing a part created locally via client

I have created a part on client only, and I want it to be removed after 1 second of being parented to game.Workspace. This would seem very simple and easy, but I have tried using the Debris:AddItem method, as well as Instance:Destroy()

I am trying to remove this part via Local Script.

I do not understand why this is not as simple as it should be.

1 Like

do you have a reference to the part, then once you have a reference, are you calling :Destroy() (or debris:AddItem) on that part?

2 Likes

A reference from another script, yes.

1 Like

hmm, could i see the code please?

2 Likes

The one referencing the part, or the one attempting to remove it?

1 Like

the one referencing the part and the one attempting to remove it

2 Likes

The local script that references it is very large, so here is the snippet that involves it.

--local part place
			
				local function makePartm()
					localpart = Instance.new("Part")
					localpart.Size = part.Size
					localpart.Material = part.Material
					localpart.BrickColor = part.BrickColor
					localpart.Position = part.Position
					localpart.Anchored = true
					localpart.CastShadow = false
					localpart.Parent = part.Parent
					localpart.BackSurface = Enum.SurfaceType.Weld
					localpart.BottomSurface = Enum.SurfaceType.Weld
					localpart.FrontSurface = Enum.SurfaceType.Weld
					localpart.LeftSurface = Enum.SurfaceType.Weld
					localpart.RightSurface = Enum.SurfaceType.Weld
					localpart.TopSurface = Enum.SurfaceType.Weld
					localpart:MakeJoints()
					localpart.Anchored = false
				end

The local script attempting to removed it (currently a mess):

local Debris = game:GetService("Debris")

while wait() do
	game:GetService("Debris"):AddItem(game.Workspace.Part, 1)
end

game.Workspace.DescendantAdded:Connect(function(part)
	print("hey")
	if part.ClassName == "Part" and part.Name == "Part" and not (part:FindFirstChild("SelectionBox")) then
		print("debris?")
		Debris:AddItem(part,1)
	end
end)
1 Like

Let me try setting the reference to nil after executing its purpose, then removing the part via the other script.

1 Like

setting it to nil was useless.

1 Like

try putting a spawn wrapping around the while wait loop, see if that works

2 Likes

Your function is never getting called

2 Likes

the makePartm() function gets called in the full script, and the part is created, as intended.

Though the other script doesnā€™t even print anything. I assume the function doesnā€™t work locally, but I canā€™t find any other way that does work locally.

1 Like

the while loop is possibly causing the workspace.DescendantAdded from not working, so, try wrapping a spawn(function() on the while loop

2 Likes

Why are you running a loop that adds a random brick named ā€œPartā€ in the workspace to debris?

2 Likes

Iā€™ve tried both of your suggestions, none workā€¦

This is now currently the remove script:

game.Workspace.DescendantAdded:Connect(function(part)
	print("hey")
	if part.Name == "TrashBlock" then
		print("debris?")
		part:Destroy()
	end
end)

This prints nothing to my log.

1 Like

Inside the DescenantAdded function put a wait() before the Part.Name check

3 Likes

Wow guysā€¦ I canā€™t believe how simple this was. The local script that was attempting to remove it, was parented to Workspace.

I moved it into my GUI element, and it started working perfectly.

I love when the little things cause such disasteršŸ˜‚

6 Likes