Instance:Destroy doing nothing

So, I have a script for adding in particles in a spot.
Here is the snippet:

-- Function to play a particle at a position for x time
function m.PlayAt(Particle, Pos, Rot, Duration, Finish)
	Finish = Finish or true
	if R:IsClient() then
		AtPos:FireServer(Particle, Pos, Rot, Duration, Finish)
		return
	end
	local P = Instance.new("Part", workspace)
	P.Anchored = true
	P.CanCollide = false
	P.Transparency = 1
	P.Position = Pos
	P.Size = Vector3.new(1,1,1)
	P.Orientation = Rot
	
	-- Actually do it
	local PC = RS.Effects[Particle]:Clone()
	local pobjs = 0
	local mpobjs = 0
	for _, v in pairs(PC:GetChildren()) do
		mpobjs += 1
		coroutine.resume(coroutine.create(function()
			v.Parent = P
			v.Enabled = true
			wait(Duration)
			v.Enabled = false
			wait(v.Lifetime.Max)
			v:Destroy()
			pobjs += 1
		end))
	end
	while pobjs ~= mpobjs do
		wait()
	end
	P:Destroy()
end

The problem I have with this is that it DOESN’T DESTROY the part. After the whole thing finishes, P:Destroy() just reverts the size and rotation and transparency so it just stays there. I also tried setting the parent to nil, and that STILL didn’t work. Any ideas?

Can you send a gif of what the issue is looking like?

Also try game:GetService("Debris"):AddItem(P)

If that doesn’t work then there’s a good chance a different issue is causing the problem.

Maybe these two never equal and so it stays on this part of the function and never gets to the destroy.

Use print statements to find which part of the code its hanging on.

They are equal eventually. I have tested that.
needs some more letters so

Try making P a global variable.

Instance:Destroy() sets the parent to nil. The part still exists while you have references to it. You can check if the part is destroyed with Instance:IsDescendantOf(workspace).

I would also double check that Destroy is running. The part does become parented to nil when Destroy is ran, though it’s possible some other code is putting it back in workspace.

I wouldn’t do this, because the OP wants to be able to create particles whenever.

2 Likes

Good point… wasn’t thinking clearly.

1 Like

I found something else.

This code:

function m.PlayInPart(Particle, Part, Duration, Finish, b)
	Finish = Finish or true
	b = b or false
	if R:IsClient() then AtPart:FireServer(Particle, Part, Duration, Finish) end
	local PC = RS.Effects[Particle]:Clone()
	local pobjs = 0
	local mpobjs = 0
	for _, v in pairs(PC:GetChildren()) do
		mpobjs += 1
		coroutine.resume(coroutine.create(function()
			v.Parent = Part
			if not b then
				v.Enabled = true
				wait(Duration)
				v.Enabled = false
			else
				v:Emit(v.Rate)
			end
			if Finish then wait(v.Lifetime.Max) end
			v:Destroy()
			pobjs += 1
		end))
	end
end

will also spawn a part for no reason…

EDIT: I forgot to mention because I jsut found out it is deleting the part but is making a new one as well… wtf.

That’s probably from this line of code:

AtPart:FireServer(Particle, Part, Duration, Finish)

It’s likely that you can’t destroy P because it isn’t parented to anything.

Try adding P.Parent = game.Workspace after declaring P’s properties.

Hope this helps!


Edit: scratch that, I’m completely blind and didn’t read your Instance:new() parameters!

Back to the drawing board…

That is just making it so the code runs from the server. I just realised i need to return, so I will do that real quick.
Also, currently, I am always running the function from the server. I tested that already.
At the END of the function is when it creates the part.

Here is a video: bug I - YouTube

It is just randomly, for no reason, creating a part after the function finishes.

Found the bug. I was stupid. I was making a part in the script that calls it.

1 Like