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

Help checking if enemy is on player's screen?

I'm trying to make a script that plays a sound when the player looks at a monster. After some research, I have this script here:

local Camera = workspace.Camera
local part = workspace.Enemies.Chaser.UpperTorso
local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)
while wait(0.1) do
if withinScreenBounds then
	print("looked")
	end
	end

The problem with this is that it only ever works after the player respawns. With the while loop it does the same thing, when the player respawns it will start looping. I've put this into a localscript located inside StarterGui. I've seen some people talking about raycasting but I'm not quite sure how to set that up. Is there any way to make the script above constantly check? Or an alternative? Any help would be appreciated.

1 comment

0

use workspace.CurrentCamera

1 answer

Your issue is you only check if the monster is on the screen once.

All you have to do is shuffle your code around so it is constantly checking whether the monster is on the screen

local Camera = workspace.Camera
local part = workspace.Enemies.Chaser.UpperTorso

while wait(0.1) do
	local _, withinScreenBounds = Camera:WorldToScreenPoint(part.Position)
	if withinScreenBounds then
		print("looked")
	end
end

1 comment

1

use workspace.CurrentCamera

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