UserInput functions wont disconnect

Basicly when I reload one gun after I have reloaded other weapon before for some resson the script didnt Disconnect the functions on the past weapon.

Script:

RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
		if isReloading == false then
			if debounce == false then
				if not gameProcessed then
					if input.UserInputType == Enum.UserInputType.Keyboard then
						local keycode = input.KeyCode
						if keycode == Enum.KeyCode.R then	
							print(tool.Name)
							debounce = true
							isReloading = true
							print(isReloading)
							CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
							reload:Play()
							Son:FireServer(Reload_Sound)
							GunParts.Mag.Transparency = 1
							local ammoClip = GunParts.Mag:Clone()
							ammoClip.Transparency = 0
							ammoClip.CanCollide = true
							ammoClip.Parent = character
							ammoClip.Position = GunParts.Mag.Position
							ammoClip.Anchored = false
							wait(1)
							GunParts.Mag.Transparency = 0
							game:GetService("Debris"):AddItem(ammoClip, 10)
							Reload_Sound.Ended:Wait()
							ammo = MaxAmmo
							reload:Stop()
							CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
							debounce = false
							isReloading = false
							print(isReloading)
						end
					end
				end
			end
		end
	end)
end

tool.Unequipped:Connect(function()
	
	if RkeyBindLister then
		RkeyBindLister:Disconnect()
		RkeyBindLister = nil
	end
	
	if button2DownListener then
		button2DownListener:Disconnect()
		button2DownListener = nil
	end
		
	if button2UpListener then
		button2UpListener:Disconnect() 
		button2UpListener = nil
	end
	
	game.ReplicatedStorage.DisconnectM6D:FireServer()
	CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

	userInput.MouseIconEnabled = true
	camera.FieldOfView = 100

	if Conn1 then Conn1:Disconnect() end

	local mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity 
	userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2

	Conn2 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
		mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity  
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2
		warn(userInput.MouseDeltaSensitivity)
	end)

	mouse.Icon = ""
	userInput.MouseIconEnabled = true

	anim:Stop()
	reload:Stop()
	ScopeAim:Stop()
end)

Full script

--------Varibles--------
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local camera = game.Workspace.CurrentCamera

--------tool parts--------
local tool = script.Parent
local GunParts = tool.GunParts
local flash = tool.GunParts.BulletLocation.Flash
local fireRemote = tool.Fire
--------UserInputService Setup--------
local userInput = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local CoreGui = game:GetService("StarterGui")
local savehere = UserGameSettings.MouseSensitivity
local configs = tool.Configuration
local newSens = 0.1
local Conn1
local Conn2

--------Animations--------
local anim
local Shoot 
local Shoot2
local reload 
local ScopeAim

local IsOnAim = false
local isReloading = false
local RkeyBindLister
--------Sounds--------
local Gun_Sound = GunParts.BodyAttach:WaitForChild("GunShoot")
local Empty_Sound = GunParts.BodyAttach:WaitForChild("EmptyGunSound")
local Reload_Sound = GunParts.BodyAttach:WaitForChild("ReloadGunSound")

--------Ammo--------
local ammo = configs.Ammo.Value
local MaxAmmo = configs.MaxAmmo.Value

--------Cooldown--------
local debounce = false
local cooldown = 0.15
local isCoolingDown = false

--------RemotesEvents--------
local Son = script.Parent.SoundOn
local Soff = script.Parent.SoundOff
local Eon = script.Parent.FlashOn
local Eoff = script.Parent.FlashOff

--------Animations Settings--------
tool.Equipped:Connect(function()
	local char = plr.Character or plr.CharacterAdded:Wait()

	game.ReplicatedStorage.ConnectM6D:FireServer(tool.GunParts.BodyAttach)

	char["Right Arm"].ToolGrip.Part0 = char["Right Arm"]
	char["Right Arm"].ToolGrip.Part1 = tool.GunParts.BodyAttach

	local Humanoid = char:FindFirstChild("Humanoid")  
	anim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
	Shoot = Humanoid:LoadAnimation(script:WaitForChild("Shoot"))
	Shoot2 = Humanoid:LoadAnimation(script:WaitForChild("Shoot2"))
	reload = Humanoid:LoadAnimation(script:WaitForChild("Reload"))
	ScopeAim = Humanoid:LoadAnimation(script:WaitForChild("Aim"))

	mouse.Icon = "http://www.roblox.com/asset?id=240472267"
	anim:Play()
end)

local function Aim()
	button2DownListener = mouse.Button2Down:Connect(function()
		if Conn2 then Conn2:Disconnect() end

		local mouseDeltaSensitivity = newSens  / UserGameSettings.MouseSensitivity 
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity

		Conn1 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
			mouseDeltaSensitivity = newSens  / UserGameSettings.MouseSensitivity 
			userInput.MouseDeltaSensitivity = mouseDeltaSensitivity
			warn(userInput.MouseDeltaSensitivity)
		end)
		ScopeAim:Play()
		IsOnAim = true
		camera.FieldOfView = 75
		userInput.MouseIconEnabled = false
	end)

	button2UpListener = mouse.Button2Up:Connect(function()

		if Conn1 then Conn1:Disconnect() end

		local mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity 
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2

		Conn2 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
			mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity  
			userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2
			warn(userInput.MouseDeltaSensitivity)
		end)
		ScopeAim:Stop()
		IsOnAim = false
		camera.FieldOfView = 100
		userInput.MouseIconEnabled = true
	end)
end

local function Pew()
	if debounce == false then
		if ammo > 0 then
			if IsOnAim == false then
				Shoot:Play()
			end

			if IsOnAim == true then
				Shoot2:Play()
			end
			camera.CFrame = camera.CFrame:Lerp(camera.CFrame * CFrame.Angles(math.rad(10),0,0),0.1)
			Son:FireServer(Gun_Sound)
			Eon:FireServer(flash)
			fireRemote:FireServer(mouse.Hit.p, tool.GunParts.BulletLocation.Position)
			ammo -= 1
			wait(0.1)
			Eoff:FireServer(flash)
		else
			if IsOnAim == false then
				Shoot:Play()
				Son:FireServer(Empty_Sound)
			end

			if IsOnAim == true then
				Shoot2:Play()
				Son:FireServer(Empty_Sound)
			end

		end
	end
	
	
 local function ReloadWeapon()
		RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
		if isReloading == false then
			if debounce == false then
				if not gameProcessed then
					if input.UserInputType == Enum.UserInputType.Keyboard then
						local keycode = input.KeyCode
						if keycode == Enum.KeyCode.R then	
							print(tool.Name)
							debounce = true
							isReloading = true
							print(isReloading)
							CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
							reload:Play()
							Son:FireServer(Reload_Sound)
							GunParts.Mag.Transparency = 1
							local ammoClip = GunParts.Mag:Clone()
							ammoClip.Transparency = 0
							ammoClip.CanCollide = true
							ammoClip.Parent = character
							ammoClip.Position = GunParts.Mag.Position
							ammoClip.Anchored = false
							wait(1)
							GunParts.Mag.Transparency = 0
							game:GetService("Debris"):AddItem(ammoClip, 10)
							Reload_Sound.Ended:Wait()
							ammo = MaxAmmo
							reload:Stop()
							CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
							debounce = false
							isReloading = false
							print(isReloading)
						end
					end
				end
			end
		end
	end)
	end
end

local function Shooting()
	if isCoolingDown == true then
		return
	end
	isCoolingDown = true
	Pew()
	wait(0.1)

	wait(cooldown)
	isCoolingDown = false
end

tool.Activated:Connect(function()
	Shooting()
end)


tool.Equipped:Connect(function()
	Aim()
end)

tool.Unequipped:Connect(function()
	
	if RkeyBindLister then
		RkeyBindLister:Disconnect()
		RkeyBindLister = nil
	end
	
	if button2DownListener then
		button2DownListener:Disconnect()
		button2DownListener = nil
	end
		
	if button2UpListener then
		button2UpListener:Disconnect() 
		button2UpListener = nil
	end
	
	game.ReplicatedStorage.DisconnectM6D:FireServer()
	CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

	userInput.MouseIconEnabled = true
	camera.FieldOfView = 100

	if Conn1 then Conn1:Disconnect() end

	local mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity 
	userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2

	Conn2 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
		mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity  
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2
		warn(userInput.MouseDeltaSensitivity)
	end)

	mouse.Icon = ""
	userInput.MouseIconEnabled = true

	anim:Stop()
	reload:Stop()
	ScopeAim:Stop()
end)

I dont really understand why the function doesnt disconnect I did print() and it shows on output but doesnt really disconnect.

If I try to only do

RkeyBindLister:Disconnect()
RkeyBindLister = nil

I will get errors.

1 Like

I think it’s because you run Pew everytime you run Shooting, which is ran everytime the tool is Activated.
So everytime you click with the tool, you are setting a new function to InputBegan

Also you can clean up your reload nesting by doing this

local function ReloadWeapon()
		RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
			if isReloading or debounce or gameProcessed then return end
			if input.KeyCode ~= Enum.KeyCode.R then return end
			
			debounce, isReloading = true, true
			print(tool.Name, isReloading)
			CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
			reload:Play()
			Son:FireServer(Reload_Sound)
			GunParts.Mag.Transparency = 1
			local ammoClip = GunParts.Mag:Clone()
			ammoClip.Transparency = 0
			ammoClip.CanCollide = true
			ammoClip.Parent = character
			ammoClip.Position = GunParts.Mag.Position
			ammoClip.Anchored = false
			wait(1)
			GunParts.Mag.Transparency = 0
			game:GetService("Debris"):AddItem(ammoClip, 10)
			Reload_Sound.Ended:Wait()
			ammo = MaxAmmo
			reload:Stop()
			CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
			debounce, isReloading = false, false
			print(tool.Name, isReloading)
		end)
	end
2 Likes

Oh god thank you I didnt look at that small issue.

The code now looks like this.

--------Varibles--------
local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local mouse = plr:GetMouse()
local camera = game.Workspace.CurrentCamera

--------tool parts--------
local tool = script.Parent
local GunParts = tool.GunParts
local flash = tool.GunParts.BulletLocation.Flash
local fireRemote = tool.Fire
--------UserInputService Setup--------
local userInput = game:GetService("UserInputService")
local UserGameSettings = UserSettings():GetService("UserGameSettings")
local CoreGui = game:GetService("StarterGui")
local savehere = UserGameSettings.MouseSensitivity
local configs = tool.Configuration
local newSens = 0.1
local Conn1
local Conn2

--------Animations--------
local anim
local Shoot 
local Shoot2
local reload 
local ScopeAim

local IsOnAim = false
local isReloading = false
local RkeyBindLister
local button2DownListener
local button2UpListener
--------Sounds--------
local Gun_Sound = GunParts.BodyAttach:WaitForChild("GunShoot")
local Empty_Sound = GunParts.BodyAttach:WaitForChild("EmptyGunSound")
local Reload_Sound = GunParts.BodyAttach:WaitForChild("ReloadGunSound")

--------Ammo--------
local ammo = configs.Ammo.Value
local MaxAmmo = configs.MaxAmmo.Value

--------Cooldown--------
local debounce = false
local cooldown = 0.15
local isCoolingDown = false

--------RemotesEvents--------
local Son = script.Parent.SoundOn
local Soff = script.Parent.SoundOff
local Eon = script.Parent.FlashOn
local Eoff = script.Parent.FlashOff

--------Animations Settings--------
tool.Equipped:Connect(function()
	local char = plr.Character or plr.CharacterAdded:Wait()

	game.ReplicatedStorage.ConnectM6D:FireServer(tool.GunParts.BodyAttach)

	char["Right Arm"].ToolGrip.Part0 = char["Right Arm"]
	char["Right Arm"].ToolGrip.Part1 = tool.GunParts.BodyAttach

	local Humanoid = char:FindFirstChild("Humanoid")  
	anim = Humanoid:LoadAnimation(script:WaitForChild("Idle"))
	Shoot = Humanoid:LoadAnimation(script:WaitForChild("Shoot"))
	Shoot2 = Humanoid:LoadAnimation(script:WaitForChild("Shoot2"))
	reload = Humanoid:LoadAnimation(script:WaitForChild("Reload"))
	ScopeAim = Humanoid:LoadAnimation(script:WaitForChild("Aim"))

	mouse.Icon = "http://www.roblox.com/asset?id=240472267"
	anim:Play()
end)

local function Aim()
	button2DownListener = mouse.Button2Down:Connect(function()
		if Conn2 then Conn2:Disconnect() end

		local mouseDeltaSensitivity = newSens  / UserGameSettings.MouseSensitivity 
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity

		Conn1 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
			mouseDeltaSensitivity = newSens  / UserGameSettings.MouseSensitivity 
			userInput.MouseDeltaSensitivity = mouseDeltaSensitivity
			warn(userInput.MouseDeltaSensitivity)
		end)
		ScopeAim:Play()
		IsOnAim = true
		camera.FieldOfView = 75
		userInput.MouseIconEnabled = false
	end)

	button2UpListener = mouse.Button2Up:Connect(function()

		if Conn1 then Conn1:Disconnect() end

		local mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity 
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2

		Conn2 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
			mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity  
			userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2
			warn(userInput.MouseDeltaSensitivity)
		end)
		ScopeAim:Stop()
		IsOnAim = false
		camera.FieldOfView = 100
		userInput.MouseIconEnabled = true
	end)
end

local function Pew()
	if debounce == false then
		if ammo > 0 then
			if IsOnAim == false then
				Shoot:Play()
			end

			if IsOnAim == true then
				Shoot2:Play()
			end
			camera.CFrame = camera.CFrame:Lerp(camera.CFrame * CFrame.Angles(math.rad(10),0,0),0.1)
			Son:FireServer(Gun_Sound)
			Eon:FireServer(flash)
			fireRemote:FireServer(mouse.Hit.p, tool.GunParts.BulletLocation.Position)
			ammo -= 1
			wait(0.1)
			Eoff:FireServer(flash)
		else
			if IsOnAim == false then
				Shoot:Play()
				Son:FireServer(Empty_Sound)
			end

			if IsOnAim == true then
				Shoot2:Play()
				Son:FireServer(Empty_Sound)
			end
		end
	end
end

local function ReloadWeapon()
	RkeyBindLister = userInput.InputBegan:Connect(function(input, gameProcessed)
		if isReloading or debounce or gameProcessed then return end
		if input.KeyCode ~= Enum.KeyCode.R then return end

		debounce, isReloading = true, true
		print(tool.Name, isReloading)
		CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		reload:Play()
		Son:FireServer(Reload_Sound)
		GunParts.Mag.Transparency = 1
		local ammoClip = GunParts.Mag:Clone()
		ammoClip.Transparency = 0
		ammoClip.CanCollide = true
		ammoClip.Parent = character
		ammoClip.Position = GunParts.Mag.Position
		ammoClip.Anchored = false
		wait(1)
		GunParts.Mag.Transparency = 0
		game:GetService("Debris"):AddItem(ammoClip, 10)
		Reload_Sound.Ended:Wait()
		ammo = MaxAmmo
		reload:Stop()
		CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		debounce, isReloading = false, false
		print(tool.Name, isReloading)
	end)
end

local function Shooting()
	if isCoolingDown == true then
		return
	end
	isCoolingDown = true
	Pew()
	wait(0.1)

	wait(cooldown)
	isCoolingDown = false
end

tool.Activated:Connect(function()
	Shooting()
end)


tool.Equipped:Connect(function()
	Aim()
	ReloadWeapon()
end)

tool.Unequipped:Connect(function()

	if RkeyBindLister then
		RkeyBindLister:Disconnect()
		RkeyBindLister = nil
	end

	if button2DownListener then
		button2DownListener:Disconnect()
		button2DownListener = nil
	end

	if button2UpListener then
		button2UpListener:Disconnect() 
		button2UpListener = nil
	end

	game.ReplicatedStorage.DisconnectM6D:FireServer()
	CoreGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

	userInput.MouseIconEnabled = true
	camera.FieldOfView = 100

	if Conn1 then Conn1:Disconnect() end

	local mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity 
	userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2

	Conn2 = UserGameSettings:GetPropertyChangedSignal("MouseSensitivity"):Connect(function()
		mouseDeltaSensitivity2 = savehere  / UserGameSettings.MouseSensitivity  
		userInput.MouseDeltaSensitivity = mouseDeltaSensitivity2
		warn(userInput.MouseDeltaSensitivity)
	end)

	mouse.Icon = ""
	userInput.MouseIconEnabled = true

	anim:Stop()
	reload:Stop()
	ScopeAim:Stop()
end)