Problem with Mouse.Target

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to print one time ONLY if the mouse hovers into a part and be able to print again when the mouse isnt hovering the part and hovers again (in the output should appear: Testing (x2))

  2. What is the issue? When the mouse enters the part theres a lot of prints including when the mouse leaves the part

  3. What solutions have you tried so far? nothing for now

btw the script that im using is:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local ToolName = "Tool"
local UserInputService = game:GetService("UserInputService")

UserInputService.InputChanged:Connect(function(input, engine_processed)
	if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if Mouse.Target:IsA("Part") and Mouse.Target:FindFirstChild("ClickDetector") and Player.Character:FindFirstChild(ToolName) then
			print("Player's Mouse is Over a part which has a Click detector and Tool equipped.")
		end
	end
end)

Add a debounce (a value meant to control if your code should be running):

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local ToolName = "Tool"
local UserInputService = game:GetService("UserInputService")

local debounce = true --<<

UserInputService.InputChanged:Connect(function(input, engine_processed)
	if engine_processed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseMovement then
		if Mouse.Target:IsA("Part") and Mouse.Target:FindFirstChild("ClickDetector") and Player.Character:FindFirstChild(ToolName) then
			if debounce == true then -- the debounce will decide if not to run this bit of code
				print("Player's Mouse is Over a part which has a Click detector and Tool equipped.")
				debounce = false -- disables the script
				
				--this part is only if you want your message to be printed out again after a certain time (used 5s as an example)
				wait(5)
				debounce = true -- re-enables the script
			end
		end
	end
end)

This will only toggle the printing, to disable the event you’ll have to disconnect it:

Be sure to reply if you need any clarification :slightly_smiling_face:

I suggest trying out @DSDamn answer

k wait bro im opening the place

Cool but i can try

elseif input.UserInputType == Enum.UserInputType.MouseIdle then
if Mouse.Target:IsA(“Part”) then
debounce = false
elseif Mouse.Target ~= workspace.Part then
debounce = true

end
no?

Debounce. At this point, it will print at every movement (Even microscopic ones)