Script isn't stopping

The script SOMEHOW isn’t breaking or stopping. It just prints “ACTIVE” all the time and it always works even if the objects don’t exist.

Help!

for _, child2 in ipairs(child:GetChildren()) do
			if string.match(child2.Name, "CannonProperty_") then
				if not child:FindFirstChild("CannonApplied_" .. string.sub(child2.Name, 16, 99999)) then
					local side = string.sub(child2.Name, 16, 99999)
					local bool = Instance.new("BoolValue")
					bool.Parent = child
					bool.Name = "CannonApplied_" .. side
					--print(side)
					coroutine.wrap(function()
						while task.wait() do
							print("ACTIVE")
							if not child then print("STOPPED") break end
							if not child2 then print("STOPPED") break end
							if child:FindFirstChild("CannonProperty_" .. side) then
								if side == "Right" and child and child2 then
									local leftDirection = Vector3.new(-1, 0, 0)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								elseif side == "Left" and child and child2 then
									local leftDirection = Vector3.new(1, 0, 0)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Front" and child and child2 then
									local leftDirection = Vector3.new(0, 0, 1)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								elseif side == "Back" and child and child2 then
									local leftDirection = Vector3.new(0, 0, -1)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Top" and child and child2 then
									local leftDirection = Vector3.new(0, 1, 0)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Bottom" and child and child2 then
									local leftDirection = Vector3.new(0, -1, 0)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								else
									print("AAAAAA")
								end
							else
								break
							end
						end	
					end)()
				end
			end
		end
1 Like

if it isn’t stopping, then the child must be continuing to exist.

Are you attempting to remove the part using the explorer to test if it stops?

1 Like

Yes, I have been. It just, doesn’t stop.

If I remove BOTH children, it will continue going with no errors.

Is it a local script or a server script?

Server script.

fillerfillerfiller

When you are deleting things in the explorer, I believe that you are only doing it from the client’s side (hence the server doesn’t see these changes). If you change the run mode from Client to Server, then attempt to delete the things from explorer, does it work as expected?

Hi, I have been switching it to server already. I know it’s an issue with the code because it only stops when I delete the parent of child.

It even prints the names when the objects are gone.

Here’s the full script context.

game:GetService("RunService").Heartbeat:Connect(function()
	if game.Workspace.Blocks:FindFirstChild("Blocks") then
		local a = game.Workspace.Blocks:FindFirstChild("Blocks")
		if #a:GetChildren() ~= 0 then
			for _, child in pairs(a:GetChildren()) do
				child.Parent = game.Workspace.Blocks
			end
		else
			a:Destroy()
		end
	end
	for _, child in pairs(game.Workspace.Blocks:GetChildren()) do
		for _, child2 in ipairs(child:GetChildren()) do
			if string.match(child2.Name, "CannonProperty_") then
				if not child:FindFirstChild("CannonApplied_" .. string.sub(child2.Name, 16, 99999)) then
					local side = string.sub(child2.Name, 16, 99999)
					local bool = Instance.new("BoolValue")
					bool.Parent = child
					bool.Name = "CannonApplied_" .. side
					--print(side)
					print("ADDED")
					coroutine.wrap(function()
						repeat
							print(child.Name)
							print(child2.Name)
							if not child then 
								print("STOPPED: Child is nil") 
								break 
							end
							if not child2 then 
								print("STOPPED: Child2 is nil") 
								break 
							end
							if child:FindFirstChild("CannonProperty_" .. side) then
								if side == "Right" and child and child2 then
									local leftDirection = Vector3.new(-1, 0, 0)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								elseif side == "Left" and child and child2 then
									local leftDirection = Vector3.new(1, 0, 0)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Front" and child and child2 then
									local leftDirection = Vector3.new(0, 0, 1)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								elseif side == "Back" and child and child2 then
									local leftDirection = Vector3.new(0, 0, -1)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Top" and child and child2 then
									local leftDirection = Vector3.new(0, 1, 0)
									child.AssemblyLinearVelocity = leftDirection * child2.Value
									task.wait(0.1)
								elseif side == "Bottom" and child and child2 then
									local leftDirection = Vector3.new(0, -1, 0)
									child.AssemblyLinearVelocity = leftDirection * -child2.Value
									task.wait(0.1)
								else
									print("Unrecognized side")
								end
							else
								print("STOPPED: CannonProperty_" .. side .. " not found")
								break
							end
						until not child or not child2    
					end)()
				end
			end
		end
		if child:FindFirstChild("IceProperty") and not child:FindFirstChild("IceApplied") then
			local ice = Instance.new("BoolValue")
			ice.Name = "IceApplied"
			ice.Parent = child
			if not child.CustomPhysicalProperties then
				local defaultFriction = 0.3
				local defaultElasticity = 0.5
				local defaultDensity = 1
				local defaultFrictionWeight = 1
				local defaultElasticityWeight = 1
				child.CustomPhysicalProperties = PhysicalProperties.new(
					defaultDensity, 
					defaultFriction, 
					defaultElasticity, 
					defaultFrictionWeight, 
					defaultElasticityWeight
				)
			end

			local currentProps = child.CustomPhysicalProperties
			child.CustomPhysicalProperties = PhysicalProperties.new(
				currentProps.Density, 
				0,
				currentProps.Elasticity, 
				100,
				currentProps.ElasticityWeight
			)
		elseif child:FindFirstChild("IceApplied") and not child:FindFirstChild("IceProperty") then
			child:FindFirstChild("IceApplied"):Destroy()
			child.CustomPhysicalProperties = nil
		end
	end
end)

Fixed: I didn’t account for if the object was removed properly and only if it was explicitly nil.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.