How do you detect a character model clicked?

I want to add an interaction system in my game by clicking the player you want to interact with. For example, I want to trade Roblox_Player123, or invite him to my home, so I click his character, press the gui popup corresponding to my desired action, and follow through in the action.

I tried creating a local character script that creates an instance of a ClickDetector and parents it to the character. The creation and parenting of the ClickDetector works, but click on the character is not detected.

I try to at least attempt every solution I can think of before relying on the wiki, but I couldn’t even think of another option. The only other option I thought of was creating a clickable billboard gui in every character and making it invisible, but even if it were to work, I don’t like that option as the gui would cover even spaces around the character such as the space between the shoulder and neck, or torso and arms.

I searched for similar problems on both youtube and the wiki… and there weren’t any topics even remotely close.

The most comparable system is the adopt me trade and gift system with character click detection.

You need to parent the ClickDetector to a “Part” object for it to work, such as the torso.

A better solution, might be to use Mouse.Target. This would allow the player to click on any part inside the character, without using a ClickDetector for each individual body part.

Example:

--LocalScript
player = game.Players.LocalPlayer
Mouse = player:GetMouse()
local function OnLeftClick()
	local Target = Mouse.Target
    if Target ~= nil then	
        if Target.Parent:FindFirstChild("HumanoidRootPart") ~= nil then
            local ClickedCharacter = Target.Parent
        	local ClickedPlayer = game.Players:GetPlayerFromCharacter(ClickedCharacter)

	        print(ClickedPlayer)
--do whatever you want with the Clicked player.
        end
    end
end

Mouse.Button1Down:Connect(OnLeftClick) -- Connect OnLeftClick() to an event

If you need to handle the trading on the server, you can send the results of this via RemoteEvent if it registers that you clicked a player.

Hope this helps!

2 Likes

This is such a flexible and simple solution, and perfect for what I’m trying to do. Thank you.

3 Likes