Find if player clicked on themselves

Currently, I’ve been using Mouse.Target to get the part player had clicked on. However, this does not work when the player had clicked on themselves, instead, it prints out the baseplate.
My question is, is there any way to check if the player had clicked on a part of themselves without using clickdetector?

Use raycasts. Raycasting | Documentation - Roblox Creator Hub

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


local plr = Players.LocalPlayer
local cam = workspace.CurrentCamera


local function mouseRaycast(params, distance) -- these are optional, not necessary
    local mousePos = UserInputService:GetMouseLocation()
    local unitRay = cam:ViewportPointToRay(mousePos.X, mousePos.Y)
    return workspace:RayCast(unitRay.Origin, unitRay.Direction*(distance or 5000), params)
end

local function getMouseTarget(params, distance) -- these are optional, not necessary
    local rayResult = mouseRaycast(params, distance)
    if rayResult then
        return rayResult.Instance
    end
    return nil
end

local function detectClick(_, inputState)
    if inputState ~= Enum.UserInputState.End then
        return
    end
    local clickedPart = getMouseTarget()
    if clickedPart and clickedPart:IsDescendantOf(plr.Character) then
        -- your code here
    end
end


ContextActionService:BindAction("Click", detectClick, false, Enum.UserInputType.MouseButton1)

@HawDevelopment the mouse will ignore the local player’s character even if mouse.TargetFilter is nil.

So I would be using raycasting to check? Could you explain it in further detail?

I edited the original code. Just call getMouseTarget when the player clicks. You can use UserInputService, ContextActionService or the mouse object to detect clicks.

Edit: I edited the code again.

Edit: Here’s some explanation. detectClick function runs everytime MouseButton1 input starts or ends. If the player releases the button (input ends), then detectClick continues and uses getMouseTarget to find out what part the mouse is pointing at. getMouseTarget uses mouseRaycast and gets the instance the ray found, if it hit something. If a part was found, it returns it. Otherwise it returns nil. Then, you get the part the mouse is pointing at. It is the same as mouse.Target would be, with the exeption that it can also be a part of the local player’s character, unlike the value of mouse.Target.

Here’s what mouseRaycast does.
mouseRaycast gets the mouse’s position on the screen. Camera:ViewPortPointToRay then creates a unit ray (unit ray = ray with length 1). The ray starts from the camera’s CFrame and it’s direction is towards where the mouse hit. It’s the same as mouse.UnitRay. Then, the the origin, and the direction multiplied by how far we want to look for parts, are given to workspace:RayCast. workspace:RayCast then returns a RaycastResult object containing information about what was hit by the ray, where the ray hit it and what is the normal of the surface the ray hit. If it doesn’t hit anything, it just returns nil.

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local char

plr.CharacterAdded:Connect(function(chararacter)
    char = character
end)

mouse.Button1Down:Connect(function()
    if char and char:FindFirstChild("Humanoid") and mouse.Target == char then
        print(plr.Name.." clicked on their character!"
    end
end)
1 Like