Attempt to index nil with disconnect

Hi guys I’m making a gun shooting system that whenever the player is holding down mousebutton 1 they would face in the direction in front of them. However I keep getting index nil with disconnect when I try to disconnect the heartbeat function after the player lets go

local script inside playercharacterscripts

if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Player.Character:GetAttribute("Equipped") then
			heldDown = true
			
			FireAnim = Player.Character.Humanoid:LoadAnimation(WeaponsAnimFolder[weapon].Fire)
			FireAnim:Play()
			
			local Weapon = char.Weapons[weapon]
			local WeaponStats = WeaponModule.GetStats(weapon)
			
			while heldDown do
				task.wait(1/(WeaponStats.Firerate))
				local StartPosition = Player.Character.Weapons[weapon].Body.Attachment.WorldCFrame.Position 
				local EndPosition = Player:GetMouse().Hit.Position

				GunShot:FireServer("Shoot", Weapon, StartPosition, EndPosition)
				local rootPart = Player.Character:WaitForChild("HumanoidRootPart")
				if rootPart then
					heartbeat = RunService.Heartbeat:Connect(function(heart)
						rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(workspace.CurrentCamera.CFrame.LookVector.X,0,workspace.CurrentCamera.CFrame.LookVector.Z))
					end)
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, typing)
	if typing then return end
	if Player.Character:GetAttribute("Equipped") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			heldDown = false
			FireAnim:Stop()
			local rootPart = Player.Character:WaitForChild("HumanoidRootPart")
			if rootPart then
				heartbeat:Disconnect()
			end
		end
	end
end)

Check if heartbeat exists before you try to disconnect it

Why not declare the heartbeat variable, reserve it in the global scope, and make sure that it exists before trying to disconnect it?

A good tip would be to do local in front of every variable. In luau, it’s a bad (and sometimes confusing or issue causing like this problem) practice to declare variable without local.

local heartbeatFunction = nil -- Reserved

--...
if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if Player.Character:GetAttribute("Equipped") then
			heldDown = true
			
			FireAnim = Player.Character.Humanoid:LoadAnimation(WeaponsAnimFolder[weapon].Fire)
			FireAnim:Play()
			
			local Weapon = char.Weapons[weapon]
			local WeaponStats = WeaponModule.GetStats(weapon)
			
			while heldDown do
				task.wait(1/(WeaponStats.Firerate))
				local StartPosition = Player.Character.Weapons[weapon].Body.Attachment.WorldCFrame.Position 
				local EndPosition = Player:GetMouse().Hit.Position

				GunShot:FireServer("Shoot", Weapon, StartPosition, EndPosition)
				local rootPart = Player.Character:WaitForChild("HumanoidRootPart")
				if rootPart then
					heartbeatFunction = RunService.Heartbeat:Connect(function(heart)
						rootPart.CFrame = CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + Vector3.new(workspace.CurrentCamera.CFrame.LookVector.X,0,workspace.CurrentCamera.CFrame.LookVector.Z))
					end)
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, typing)
	if typing then return end
	if Player.Character:GetAttribute("Equipped") then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			heldDown = false
			FireAnim:Stop()
			local rootPart = Player.Character:WaitForChild("HumanoidRootPart")
			if rootPart then
				if heartbeatFunction ~= nil then
					heartbeatFunction:Disconnect()
					heartbeatFunction = nil
				end
			end
		end
	end
end)

I mean it should since the hearbeat was added in an inputstarted i forgot to show that

just check anyways. Ya never know
Also try what @MisterGrinch2017 said.
Also you have a while loop that sets heartbeat, wouldnt it just be good to just set it once and not every time the while loop triggers?

I already had it in a global scope, something I just added was another bool value specfically for the while loop and for some reason I dont get the error anymore but the hearbeat doesnt seem to be disconnecting

wait… that completely crossed my mind I dont need a hearbeat when its in a while loop… i copied this hearbeat function from some other forum topic and I didnt consider that

1 Like

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