How to run a script whenever a player is looking at a part

Hello, I’m making a game and wondering how to run a script when a player is looking at a certain part. My game is a first-person game. I tried looking on most forum posts, but none seem to work nor do I have any idea how to code this. Thanks in advance.

local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)

if withinScreenBounds then
      
end
1 Like

Does the script need to be a local script or a normal script and where does it need to be placed?

This would be a localscript, camera would be game.Workspace.CurrentCamera, and this would be placed in a RunService loop preferably so it checks continuously. You can place it in any game folder that replicates to the client, such as StarterPlayerScripts.

local camera = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")

runService.RenderStepped:Connect(function()
    local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)

    if withinScreenBounds then
      
    end
end
2 Likes