How to make a script that when a player looks at a brick, but looks away, the brick disappears? I've considered finding the lookvector of the character torso, but I don't know how to use it.
This can be a good use of mouse.Target. Target will tell you if the mouse is touching the part. So you can simply wait until the part is the target, then wait until the part is not the target.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local part = workspace:WaitForChild('Part') while true do part.Transparency=0.5 repeat wait(1/3) until mouse.Target == part print('Looking at part') part.Transparency=0 repeat wait(1/3) until mouse.Target ~= part print('Stopped looking at part') end
I'm interested to know a better way, but this is the simplest way I can think of.
local player = game.Players.LocalPlayer local cam = Workspace.CurrentCamera local part = Workspace.Part cam.Changed:connect(function() local pos, visible = cam:WorldToScreenPoint(part.Position) -- Tells you where the brick is on your screen and whether it's visible to the user -- If the brick is anywhere on your screen, then you're looking near or at it. part.Transparency = visible and 0 or 1 -- if you aren't looking at it, then make it transparent, if you are, make it visible. end)