Not disconnecting

What I want to happen is when ever the player is not touching the part, runServ.Heartbeat is disconnected. However, this is not the case.

code inside a Server Script

--Mainline
plr.PlayerAdded:Connect(function(player) -- when player joins
	player.CharacterAdded:Connect(function(char) -- when the player's character is loaded in
		local humanoidRootPart = char:WaitForChild("HumanoidRootPart") -- wait for HumRP to load in
		local plrHead = char:WaitForChild("Head")
		local looking
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {dummy, dFOV,}
		raycastParams.IgnoreWater = true
		dFOV.Touched:Connect(function()
			looking = runServ.Heartbeat:Connect(function()
				local raycastResult = workspace:Raycast(rayOrigin, plrHead.Position - rayOrigin, raycastParams)
				if raycastResult and raycastResult.Instance then
					if raycastResult.Instance == plrHead then
						dummy:SetPrimaryPartCFrame(CFrame.lookAt(dummyPos, humanoidRootPart.Position * Vector3.new(1,0,1) + dummyPos * Vector3.new(0,1,0)))	
					end	
				end	
			end)
		end)
		dFOV.TouchEnded:Connect(function(hit)
			print(hit.Parent)
			print(player)
			if hit.Parent == player then
				looking:Disconnect()
			end
		end)
	end)
end)

print statements are for debugging (forgot to remove, please ignore them)

Will check when I get home from school

What is your case then though?

Replace hit.Parent == player to hit.Parent == char

Try this

--Mainline
plr.PlayerAdded:Connect(function(player) -- when player joins
	player.CharacterAdded:Connect(function(char) -- when the player's character is loaded in
		local humanoidRootPart = char:WaitForChild("HumanoidRootPart") -- wait for HumRP to load in
		local plrHead = char:WaitForChild("Head")
		local looking
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.FilterDescendantsInstances = {dummy, dFOV,}
		raycastParams.IgnoreWater = true
		dFOV.Touched:Connect(function()
			looking = runServ.Heartbeat:Connect(function()
				local raycastResult = workspace:Raycast(rayOrigin, plrHead.Position - rayOrigin, raycastParams)
				if raycastResult and raycastResult.Instance then
					if raycastResult.Instance == plrHead then
						dummy:SetPrimaryPartCFrame(CFrame.lookAt(dummyPos, humanoidRootPart.Position * Vector3.new(1,0,1) + dummyPos * Vector3.new(0,1,0)))	
					end	
				end	
			end)
		end)
		dFOV.TouchEnded:Connect(function(hit)
			print(hit.Parent)
			print(player)
			if hit.Parent == char and looking then
				looking:Disconnect()
				looking = nil
			end
		end)
	end)
end)

Sorry for the late reply, the code you have given me does not work. It does the same thing as my code.

runServ.Heartbeat does not disconnect, and the dummy continues to look at the player

What I want the code to do is whenever the player is inside the dummy’s FOV, the dummy will track the player until the player is out of the FOV or out of view due to a wall or any object obscuring the dummy’s view. Currently, the code that I have provided will track the player while inside the FOV and stop tracking when the player is hiding behind an object. However, when I move out of the dummy’s FOV, it still tracks me.

Youtube video showing what happens:

It doesn’t track you, but it’s stuck on the last time it saw you.

I want it to be stuck on the last time it sees the player. I just don’t want it to look at me whilst outside of the FOV range

As i see in the video it does exactly that, could you show me the difference between your script and my script through videos?

Just to make sure if my script solved it, or needs updating.

Your code:

Looks like its working, when you leave the dummy’s fov its stuck on the last time it saw you.

Yes, that is true when the player is hiding behind an object (in the video the wall). However, the problem here is that the player, who is outside of the FOV triangle (the transparent triangle attached to the dummy’s head), is still being tracked. I don’t want this to happen.