Detect if a player looks at a certain part

How can I tell if a player looks at a certain part? Please let me know.

This should be in #help-and-feedback:scripting-support.

Also, I have a script that does exactly this:

--Works with models and parts!

local player = game.Players.LocalPlayer --Gets the player the script is running for.
local mouse = player:GetMouse() --Gets the player's mouse.

mouse.Move:Connect(function() --Shoots a function whenever the players' mouse moves.
	local target = mouse.Target --Gets whatever object the mouse is pointing at.
	if not target then --If there is nothing selected.
		player.PlayerGui.DisplaySelection.TextLabel.Text = "" --Changes the GUI text to display nothing when not selected.
	else --If it is a target.
		if target:IsA("BasePart") or target:IsA("Model") then --If the part the mouse is hovering over is actually a part or a model.
			if target == workspace.SpawnLocation or target.Parent == workspace.Keycard or target == workspace.boom then --If the mouse is hovering over the desired parts you want it to select. Replace with your parts.
				if target.Parent == workspace then --If it is a part and not a model.
					player.PlayerGui.DisplaySelection.TextLabel.Text = target.Name --Changes the GUI text to whatever the parts' name is.
				else --If it is a model
					player.PlayerGui.DisplaySelection.TextLabel.Text = target.Parent.Name --Changes the GUI text to whatever the models' name is.
				end
			else --If it is not any of the listed above.
				player.PlayerGui.DisplaySelection.TextLabel.Text = "" --Changes the GUI text to display nothing when not selected.
			end
		end
	end
end)

This is a script where if you look at a model or part of your choice, it displays what it is in a TextLabel (U have to add the text label yourself)

Place in StarterPack or StarterGUI as a local script.

2 Likes

Ah crap, seems like I selected the wrong category while creating this topic. Anyways, thank you so much!

1 Like

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