Check sight script! (Hoping to improve efficiency!)

So basically, I made a simple script to check whether the player can see something or not!

Its inefficient, so I’m hoping you guys can give tips on how to simplify it!

	local vector, onScreen = workspace.CurrentCamera:WorldToScreenPoint(instance.Position)
	if not onScreen then
		return false
	end
	local ray = Ray.new(LocalPlayer.Character.Head.Position, (instance.Position - LocalPlayer.Character.Head.CFrame.p).unit * 100)
	local part, position = workspace:FindPartOnRayWithIgnoreList(ray, {LocalPlayer.Character}, false, true)
	
	local InSight = part and part:IsDescendantOf(instance.Parent)
	
	if instance.Parent == game.Workspace and part ~= instance then
		return false
	end
	
	return InSight

It looks about as good as you can get. How often is this running, and is it causing performance issues?

We’re doing this on a loop-ish, depends on whats going on in the game.

The efficiency is great, but I feel its kinda long, and not so readable, you know?

You haven’t been browsing the scripts around here, have you?
No, that’s clean and easy to follow. It’s about as simple as raycasting gets, except for the fact that FindPartOnRayWithIgnore list is deprecated. I don’t know how the new method works yet though.

Ah its deprecated? I actually never knew! Thanks for telling me.

Not just that, the whole system of raycasting that way.
I believe this is the new system. WorldRoot | Documentation - Roblox Creator Hub

2 Likes

Hmm, I’ve looked at that, I don’t quite understand it completely. But thanks anyways, gonna mark this as solved.