Is there any better way to make hitscan?

I’ve been working on a shooter game project of mine.
I was working on the “hitscan” system, but the method i used
is not optimized, i noticed that every time i run the hiscan function, my fps drop from
60 to 10.
I would like to know how popular games do it or at least how do i optimize this so it doesnt
causes lag spikes.

This is an example of what i have for now:

local ray =,,0,0))
local ignorelist = {}
for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Part") then
		if v.CanCollide == false then

local part,position = workspace:FindPartOnRayWithIgnoreList(ray,ignorelist)

I’ve also heard that raycasting is being deprecarted.

1 Like

Yeah, the lag is caused by the workspace get descendants function.

Every time you run the function you are telling the workspace to get all of its descendants. That means you are obtaining everything of the workspace such as all the accessories of the character, humanoid, limb parts, upper torso, gun model, the map everything within the workspace at the moment the code was ran.

Normally to avoid telling the computer to find these parts to ignore we predefine what we want to ignore before the function stored within memory such within a table or with the newer raycast the collision group system.

And yes as you mentioned this function for raycasting is being deprecated.


The reason being is that the newer raycast function has more versatile parameters to fine-tune the properties of the raycast such as working with the new collision group system which you can research on your own. I suggest reading the dev reference API to confirm your suspicions on raycasting and doing research as well.

1 Like