Issue with :Disconnect()

Hi.
So i’m new with the :Disconnect() function and i’m trying to learn how to use it.
Yesterday, I made a script using this function but it doesn’t seem to be working.

The issue is that ;
I made 2 of these scripts (see below) but both of the scripts still print "Hello"


Both of the scripts are the exact same (duplicated)

local connection


game:GetService("RunService").RenderStepped:Connect(function()
	
	if tank.Value == "Tank1" then
		connection = mouse.Button1Down:Connect(function()
			holding = true
			while holding do
				print("Hello")
				
				mouse.Button1Up:Connect(function()
					holding = false
				end)
				
			end
		end)
	end
	
	if tank.Value ~= "Tank1" then
		connection:Disconnect()
	end
	
end)

Thanks for your help!
I’m bad at connections ;-;

I think you should use the Disconnect function inside of the connection. I believe you should also make the connection nil.

Try using this code for your script:

local connection


game:GetService("RunService").RenderStepped:Connect(function()
	
	if tank.Value == "Tank1" then
		connection = mouse.Button1Down:Connect(function()
			if tank.Value ~= "Tank1" then
				if connection then
					connection:Disconnect()
					connection = nil
				end
			end

			holding = true
			while holding do
				print("Hello")
				
				mouse.Button1Up:Connect(function()
					holding = false
				end)
			end
		end)
	end
end)
1 Like

One thing to mention is that your code has a serious memory leak:

You’re connecting a new event every time the loop runs and never disconnect it later. This means that when the event fires, every connection will run as well, causing a decrease in performance.

What you should do is this:

mouse.Button1Up:Wait()
holding = false

This waits for the event to fire then sets holding to false.

2 Likes

So I did what you told me to but both of the scripts print "Hello". Only one code should be printing this if the tank.Value == [value]

Current script
local connection


game:GetService("RunService").RenderStepped:Connect(function()
	
	if tank.Value == script.Parent.Name then
		connection = mouse.Button1Down:Connect(function()
			
			if tank.Value ~= script.Parent.Name then
				if connection then
					connection:Disconnect()
					connection = nil
				end
			end
			
			holding = true
			while holding and not debounce do
				debounce = true
				print(script.Parent.Name.." is shooting.")
				remote:FireServer((bulletDamage.Value + 1), bulletSpeed.Value, bulletSize.Value, tank.Value)
				local knockback = Instance.new("BodyPosition")
				knockback.MaxForce = Vector3.new(5000,0,5000)
				knockback.P = 2000
				knockback.D = 1000
				knockback.Position = (humanoidRootPart.CFrame * CFrame.new(0,0,3)).Position
				knockback.Parent = humanoidRootPart
				coroutine.wrap(function()
					wait(0.2)
					knockback:Destroy()
				end)()
				mouse.Button1Up:Connect(function()
					holding = false
				end)
				if reload.Value <= 0 then
					wait(0.75)
				end
				if reload.Value == 1 then
					wait(0.67)
				end
				if reload.Value == 2 then
					wait(0.59)
				end
				if reload.Value == 3 then
					wait(0.51)
				end
				if reload.Value == 4 then
					wait(0.43)
				end
				if reload.Value == 5 then
					wait(0.35)
				end
				if reload.Value == 6 then
					wait(0.27)
				end
				if reload.Value == 7 then
					wait(0.19)
				end
				debounce = false
			end
		end)
	end
	
end)

Don’t forget that there are 2 of this code