How to check if mouse entered a player's Humanoid

Hihi! So am tryna make a Gun System but I cant figure out how do I make the cursor icon red when the mouse hovers over a player/character/humanoid.

Is there any way I could do this?

1 Like
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Target = Mouse.Target
	
if Target and Target.Parent:FindFirstChild("Humanoid") then
	print(Target)
end

And you just need to have a loop or renderstepped checking mouse target.

3 Likes

Or attach it to a Mouse.Move event so it doesnt fire as much as renderstepped

1 Like

If you do this a player that moves in front of the mouse when the mouse isn’t moving won’t change the cursor icon.

Fair enough, you would need a loop so the mouse.Target updates regularly

Took @realmile 's script and modified it a bit so it finds a humanoid in the model the mouse is pointing too

local Run = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
	
Run.RenderStepped:Connect(function()
   local Target = Mouse.Target
   if Target and Target:FindFirstAncestorOfClass("Model") then
     local model = Target:FindFirstAncestorOfClass("Model")
	 print(Target)
     if model:FindFirstChild("Humanoid") then
        --do stuff
     end
   end
end)
3 Likes

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Camera = workspace.CurrentCamera

local FilterParams = RaycastParams.new()
FilterParams.FilterDescendantsInstances = {Player.Character}

RunService.RenderStepped:Connect(function()
	local Ray = Camera:ViewportPointToRay(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)

	local Result = workspace:Raycast(Ray.Origin, Ray.Direction * 1000, FilterParams)

	if Result then
		local Target = Result.Instance
		local Character = Target:FindFirstAncestorWhichIsA("Model")
		if Character then
			local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
			if Humanoid then
				--its a player
			end
		end
	end
end)

This should be all you need. It uses UserInputService instead of the mouse object as the mouse object has been superseded by that service.

3 Likes

The only issue with this is that it only works in first person since it tracks middle of the screen instead of mouse position, im not sure if you could even find mouse position without mouse object :\

1 Like

UserInputService:GetMouseLocation()

Their not gonna supersede something without replacements.

2 Likes

Ah thanks a lot man! Ima try this now :slight_smile: