Unable to make loop stops

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make this loop ends when the LineDrawer is gone.
  2. What is the issue? Include screenshots / videos if possible!
    It doesn’t end even though.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to put conditions and use any kinds of loop (of course not the for - loop) but it stills continues even when the part was destroyed. I looked up some posts on DevForum and it doesn’t help me much with this problem.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Module.MainLine = function(Humr, MousePos)
	local SP = Humr.Position + Humr.CFrame.LookVector * 5
	local MidP = (Humr.CFrame.LookVector * 20) + Vector3.new(0, 120, 0)
	
	local LineDrawer = Instance.new("Part")
	LineDrawer.Name = "LineDrawer"
	LineDrawer.Position = SP
	LineDrawer.Size = Vector3.new(1, 1, 1)
	LineDrawer.CanCollide = false
	LineDrawer.Anchored = true
	LineDrawer.Transparency = 0
	LineDrawer.Parent = workspace		
	
	task.spawn(function()
		while LineDrawer ~= nil do
			task.wait()
			local NewBall = Ball:Clone()
			local RotateX = math.rad(RD:NextInteger(-180, 180))
			local RotateY = math.rad(RD:NextInteger(-180, 180))
			local RotateZ = math.rad(RD:NextInteger(-180, 180))
			NewBall.CFrame = LineDrawer.CFrame * CFrame.Angles(RotateX, RotateY, RotateZ)
			NewBall.Parent = workspace
		end
	end)
	
	for i = 0, 1, .01 do
		task.wait(.01)
		local Quad = BrezierModlue.QuadBrezier(i, SP, MidP, MousePos)
		LineDrawer.Position = Quad
		
	end
	
	LineDrawer:Destroy()
end

I had many problems when working on with other loops (not for - loop), so thank you if you can correct me.

Which loop are you talking about in particular?

It’s the while loop, I had many problems with while and repeat loops too.

1 Like

while LineDrawer ~= nil do right?

1 Like

Change

To

while LineDrawer.Parent do
1 Like

Yea that should work try that!

1 Like

Thanks for the help, I wanted to know another thing, why didn’t it work if I put LineDrawer itself?

Because LineDrawer still existed but was in nil.

like I said in the topic, I put LineDrawer ~= nil then but it didn’t work

Its not the LineDrawer that’s nil. It’s the parent.

1 Like

Destroying instances doesn’t set their value to nil, they are actually paranted to nil, Basically if you destroy an instance it will still has its properties and everything but it will be parent to nil

3 Likes

Thanks you both for your helps, it will help me a lot in the future