Need help with :Disconnect()

Hi, I was trying to make a damaging ball of magic that follows players and I encountered this issue: in the following script, connection:Disconnect() does not work.

connection = game:GetService("RunService").Heartbeat:Connect(function()
			Target = workspace:FindFirstChild("Enemy")
			
			if not Target then
				warn("No target")
				
				Ball:Destroy()
				
				for i, v in pairs(game.Workspace:GetDescendants()) do
					if v:FindFirstChild("Creator") then v:Destroy() end
				end
				
				connection:Disconnect()
			end

If you know why, please let me know. Thank you!

1 Like

Does it error?

(Also please avoiding workspace:GetDescendants() if possible. If you have a particularly large map this will cause large lagspikes. Consider using CollectionService)

1 Like

It doesn’t error or warn anything, it just continues to run forever.

okay, follow up: does “No target” print in the console?

Maybe this is why, it could be perhaps the if statement isn’t running, but if it is. It could be because of that for loop, as the script will wait for the for loop to be finished and THEN disconnect it so maybe try putting the event:Disconnect() above the for loop.

Yes it does, it prints again and again and again. (Heartbeat)

1 Like

Try printing the value of Target before the if statement.

a real head scratcher. you’re not redefining connection anywhere are you?

I see you’re not using local connection. since it’s a common variable name chances are you defined it somewhere else in your script as well.

2 Likes

Here’s the whole script:

local BaseBall = game.ReplicatedStorage:WaitForChild("Ball")
local Event = game.ReplicatedStorage:WaitForChild("SendMagic")

local Damage = 25
local MaxDuration = 10
local Speed = 1
local Accelerartion = 1.1

Event.OnServerEvent:Connect(function(player)
	local Ball = BaseBall:Clone()
	Ball.Parent = workspace
	Ball.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position + (player.Character.HumanoidRootPart.CFrame.LookVector*2), player.Character.HumanoidRootPart.Position)
	Ball.Size = Vector3.new(0.1,0.1,0.1)
	Ball.Anchored = true
	
	local Tag = Instance.new("StringValue")
	Tag.Name = "Creator"
	Tag.Value = player.Name
	Tag.Parent = Ball
	
	local HasDamaged = Instance.new("BoolValue")
	HasDamaged.Name = "HasDamaged"
	HasDamaged.Value = false
	HasDamaged.Parent = Ball
	
	HasDamaged:GetPropertyChangedSignal("Value"):Connect(function()
		if HasDamaged.Value == true then
			if Ball then Ball:Destroy() end
		end
	end)
	
	wait(0.5)

	Ball.Anchored = false
	
	local CurrentSpeed = Speed
	
		connection = game:GetService("RunService").Heartbeat:Connect(function()
			Target = workspace:FindFirstChild("Enemy")
			
			if not Ball then
				warn("No ball")
				connection:Disconnect()
			end
			
			if not Target then
				warn("No target")
				
				Ball:Destroy()
				
				connection:Disconnect()
				
				for i, v in pairs(game.Workspace:GetDescendants()) do
					if v:FindFirstChild("Creator") then v:Destroy() end
				end
			end
			
			if HasDamaged == true then
				warn("HasDamaged")
				
				Ball:Destroy()
				connection:Disconnect()
			end

			if (Target.HumanoidRootPart.Position - Ball.Position).Magnitude <= 1 then 
				warn("Magnitude <= 1")
				
				Ball:Destroy() 
				Target.Humanoid:TakeDamage(Damage)	
				connection:Disconnect()
			end

			local Direction = (Target.HumanoidRootPart.Position - Ball.Position).unit

			CurrentSpeed = CurrentSpeed + Accelerartion

			Ball.Velocity = Direction * CurrentSpeed
		end)
		
		local debounce = false

		Ball.Touched:Connect(function(hit)
			if debounce or Ball.HasDamaged.Value == true then return else debounce = true end
			
			if not Target then Ball:Destroy() return end

			if hit:FindFirstAncestor(Target) then				
				Ball.HasDamaged = true
			
				workspace.Enemy.Humanoid:TakeDamage(Damage)
			end
			
			debounce = false
		end)

		spawn(function()
			wait(MaxDuration)
			if Ball then Ball:Destroy() end
			connection:Disconnect()
		end)

Could you add a print statement under this line Event.OnServerEvent:Connect(function(player) to see if this is running before the connection is disconnected?

I’m still homing in on the possibility of connection being redefined before your code tries to disconnect it.

I’m not seeing a local connection = nil before you define connection, which means it’s overriding an existing connection variable. Try defining the variable as nil right before you assign it.

3 Likes

image
I’ll try TestyLike3’s idea.

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