Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Detect if mouse is on a player?

Hello. I found through ROBLOX many games where a selectionbox appeared around a character if you had the mouse on the character, and I now need to use that into my game. I made this script....

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
 
local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.fromRGB(255, 0, 0)
selection.Parent = script
 
mouse.Move:connect(function()
	print ("MouseMoved!")
	local target = mouse.Target
 
	if not target then
		selection.Adornee = nil
	else
		print ("Target is ".. target.Name)
		if target.Parent:IsA("Model") then
			print ("Model!")
			local findhumanoid = target.Parent:FindFirstChild ("Humanoid")
			if findhumanoid ~= nil then
				print ("Humanoid!")
				local findplayer = game.Players:FindFirstChild (findhumanoid.Parent.Name)
				if findplayer ~= nil then
					print ("player!")
					script.SelectedPlayer = findplayer
					selection.Parent = target.Parent
					selection.Adornee = target.Parent
					print ("DONE!")
				end
			end
		end
	end
end)

The script worked fine, with nothing in the output, but, when I held the mouse on a character, no selectionboxes appeared at all. I placed some prints, and I noticed that ROBLOX actually can't detect if the mouse is on a character because the mouse.Target can NOT detect Meshes! I wonder what should I do. What if I create a script that creates a square part on every character and the script, instead of detecting character's meshes, detects the part? Or maybe there is another way? Any help appreciated!

1 answer

✓ Accepted answer

Use the UnitRay property of the mouse. It should work if you want the player to be detected.

local target = workspace:FindPartOnRay(
		Ray.new(mouse.UnitRay.Origin,mouse.UnitRay.Direction*50000)
)

Also, add an ignore list if necessary.

3 comments

0

Yeah, but it seems like It's not detecting the head. It's logical, It's amesh inside a part, but, anyway, any ideas of detecting the head?

0

(I will accept your answer anyway if you can't answer to that)

0

Is there a hat over the head? If there is, then that may be the problem. Otherwise, it should work.

This discussion is preserved as it was. New answers, comments, and votes are closed.