How do i detect if the player is looking at an NPC

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

  1. What do you want to achieve? Keep it simple and clear!
    i want to detect if the player is looking at an NPC
  2. What is the issue? Include screenshots / videos if possible!
    i dont know how ive tried look vector and dot product but i dont know ho wto use them for the thing i want to achieve
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    again. i have tried lookvector and dot product and worldtoviewportpoint

To clarify, do you mean if the NPC is in the player’s camera or if the player is facing the NPC?

2 Likes

if the player is facing / has the mouse pointer on the npc no specific parts. just the npc in general

Those are completely different.

Having the player face the NPC would likely use Raycasting to find whatever is in front of them, while having the mouse pointer hover over the NPC would likely use Mouse.Target (Mouse | Roblox Creator Documentation).

doesnt mouse.target need specific [arts of the body like legs or arms to detect the NPC?

Mouse.Target just gives you what the Mouse is pointing at. If you take the parent of a humanoid’s Left Leg or it’s Right Arm, it’ll be the same answer. Just make sure to check for a humanoid inside the parent.

Okay ive made something like this but it only works using a tool how do i made so it so the player doesnt need a tool and is always checking if the player is looking at the NPC

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		local target = mouse.Target
		if target.Parent:FindFirstChild("Humanoid") and target.Parent ~= workspace then
			print(target.Parent.Name)
			script.Parent.RemoteEvent:FireServer(target.Parent)
		end
	end)
end)

while true do, RenderStepped, anything that runs constantly would work. The world is yours! Although, ideally, you’d only want to run the script when Mouse.Target changes.

Oh, actually, may I make a suggestion?
ClickDetectors also have the MouseHoverEnter event. Just put a big box around your NPC that’s invisible and can’t be collided with, put a ClickDetector with no icon in there, then make a script connect the Event together.

hmm i have a question where can i put the localscript since its not in a tool anymore and is not meant for a tool

im guessing character model? or the playersscripts

I’d go for StarterCharacterScripts personally.

Workspace.thepower19012.LocalScript:7: attempt to index nil with ‘Parent’

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local cooldown = 0.1
while true do
	wait(cooldown)
	mouse.Button1Down:connect(function(mouse, target)
		if target.Parent:FindFirstChild("Humanoid") and target.Parent ~= workspace then
			print(target.Parent.Name)
		end
	end)
end

I wouldn’t recommend using StarterCharacterScripts as this can build up game memory over time. You could just put it inside StarterPlayerScripts and use connections to check when the player has died. Plus it can be useful if u have previous values set that u don’t want to be erased or If u wanted to safely send remotes.

how do i make connections i mean i do know how to but when the player has died if you a link to document or something id be happy to read that.

I’m not exactly sure what you’re asking but I would either use player.CharacterAdded for when they have respawned and you can use player.CharacterRemoving for when they are de-spawning.

You can also use player.Character.Humanoid.Died if you want but I wouldn’t recommend it.
Refer to: PlayerClass

This is the solution(make a part inside the NPC and size it to be a bit bigger then the NPC character and weld it or motor6d it to the torso of the NPC and then make it invisible and non collidable and add a click detector and remove the icon):

-- local script in StarterCharacterScripts
local clickdetect = game.Workspace.Dummy.Parttocheck.ClickDetector

clickdetect.MouseHoverEnter:Connect(function()
	print(clickdetect.Parent.Parent.Name)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.