How to make a gui appear when something (or someone) is being looked at?

hi there, I’ve posted about this before but I figured I’d do it again since I didn’t really understand how to do it so I’m asking again to see if I can get more advice I could understand easier.

again, I’m trying to make a slender man game on Roblox, so I wanna make a static GUI appear once slender man is being looked at by a player. Any help is greatly appreciated since nobody else wants to help

thank you in advance

You can use raycast to know when someone is looking

You could raycast from the Player’s PrimaryPart and check if the ray is intersecting with a part of the Slenderman

everyone’s telling me about raycasts, problem being i have never worked with them before at all so i have no idea where to start

everyone’s telling me about raycasts, problem being i have never worked with them before at all so i have no idea where to start

You could use WorldToScreenPoint to check if “slender’s” position is visible on the players screen. However, it will still return true even if the point is blocked by parts or terrain. If this isn’t what you want, you’ll need to lookup and learn raycasting.

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint

Below is just a dummy with a humanoidRootPart in workspace.
Example of it in use:

-- This is a local script in startergui
-- Variables --
local WS = game:GetService("Workspace");
local Camera = WS:WaitForChild("Camera");

local DummyRootPart = WS.Dummy.HumanoidRootPart; -- For this example we want to see if the dummies humanoidRootPart is visible on the players screen

-- Loop --
while wait()do -- I'm using a while loop for the example
	local Position, CanSee = Camera:WorldToScreenPoint(DummyRootPart.Position); -- WorldToScreenPoint returns a vector3 and bool value. You really only need to use the bool value.
	if CanSee then 
		print("I can see the dummy. Do random UI effectsz");
	else
		print("Can't see the dummy. Do nothing or disable effects");
	end;
end;

Hopefully this helps and goodluck.

2 Likes

how would i be able to use Camera:WorldToScreenPoint to toggle the GUI?

See in my example where it shows print("I can see the dummy. Do random UI effectsz"). You can quite literally just do MyStaticGui.Visible = true and where print("Can't see the dummy. Do nothing or disable effects"), just put do MyStaticGui.Visible = false.

Note that MyStaticGui is the reference to the gui you want to show.

ah alright, i’ll try this. Thank you. I’ll update you on the situation

where would i have to put the script though? inside the dummy?

The example script was a local script in starterGui. You could just put it in the UI itself (assuming the GUI is in StarterGui or any other place where local scripts can run).

ah i didn’t see that :sweat_smile:
thanks for the help though

hey! it worked, but it still triggers through walls, any way to fix?

Unfortunately, as I mentioned before, you would have to use raycasting if you want to achieve that.

alright, well i’ll look into it. Thank you so much for the help though! :slight_smile: