Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Parts that know when the player is/isn't looking at them?

I've seen it done on games before so I know it's possible.

I'm trying to create a scenario where when the player looks at an object, looks away, and looks back again, the object is removed/destroyed. I've tried using a local script, and using the function WorldToScreenPoint() with the camera but I failed tragically.

How would I accomplish this?

My failed attempt that gave me the error "Unable to cast array to Vector3"

local cam = workspace.CurrentCamera

while true do
	if cam:WorldToScreenPoint({-28, 0.6, 25}) == true then --checking if anyone is looking at camera
		repeat wait() until cam:WorldToScreenPoint({-28, 0.6, 25}) == false -- waits until they aren't looking at the camera
		game.Workspace.Part:Destory()
	end
end

1 comment

0

Good Question.

1 answer

✓ Accepted answer

Using the Camera method is a good idea, do you still have the code with it? If I was in this scenario I would use it. Sorry I cannot use the comments yet, so if you do paste it, ill edit this post again.

EDITED (6:42) - the error you got is because it returns 2 values, a Vector3 and a boolean EDITED (6:44) - Just looked at your code again, the argument for it needs to be a Vector3 also

--[[
It seemed to work for me, but remember it is an example, it is not meant to be good I guess.
]]
local player = game:GetService("Players").LocalPlayer
local workspace = game:GetService("Workspace")
local camera = workspace:FindFirstChild("Camera") --you could do workspace.CurrentCamera
local test = workspace:FindFirstChild("TestPart")
local deb = false 
local counter = 0 

function Looked()
	if (not camera) then return end
	if (not test) then return end
	local a, b = camera:WorldToScreenPoint(test.CFrame.p)
	
	if (b == true) then 
		deb = true 
	end 
	
	if (b == false) then 
		if (deb == true) then 
			counter = counter + 1
			print("Looked af") 
		end 
		deb = false 
	end 
end 

game:GetService("RunService").RenderStepped:connect(function()
	Looked()
	if (counter == 2) then --depending on whether you want them to look at it again before it disappear or disappears after one look depends 
		test:Destroy()
	end
end)

my comment did not complete

Theres one thing wrong with it, you should disconnect the event when (RenderStepped) when the part is no more, and instead of upvaluing the test variable from the main scope, you could pass it as an argument in the function

4 comments

0

Ill make an example

0

I tried it on a new place that I didn't save. But I can make the code again quickly

0

also

0

The error you got is because it returns 2 results

This discussion is preserved as it was. New answers, comments, and votes are closed.