Hit Detection/Bullet Firing

I haven’t created very many hit detection systems before, but I’m looking to start out of semi-necessity, because me and a group of others are working on a game and it requires guns.

Here’s my problem: I plan do this on the server where many bullets will be handled, probably across many threads (multithreading maybe necessary because the function I’m using naturally yields…?). Something tells me that this isn’t exactly the greatest solution in terms of performance. In fact, I forsee gamebreaking lag if more than 3 people start shooting rapidly at once.

Am I correct in concerns about performance? If so, does anyone have any suggestions on how to make this more performant?

Here’s the code:

local DefaultSpeed = 100

local Bullet = {}

function Bullet.fire(Origin, Direction, StartTick, IgnoreList)
	local PreviousTick = StartTick
	local Result
	
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = IgnoreList
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.IgnoreWater = false
	
	repeat
		Result = workspace:Raycast(
			Origin,
			Direction.Unit * DefaultSpeed * (tick() - PreviousTick),
			Params
		)
		
		PreviousTick = tick()
	until Result
	
	return Result
end

return Bullet

Why are you raycasting in a loop?
It will obviously cause performance issues.

You’ve probably tried to make a bullet with a travel time but you’re not even using any wait function.

If you want to achieve a gun system with the bullet travel time, you should definitely check out a module called FastCast which provides not only this but also way more interesting features.

1 Like

Understood. I’d be interested to see how FastCast handles that.

Thanks for calling me out on that running raycasts in a loop thing, I’ve created a new system to achieve the same goal, stress-tested it, and it actually has pretty solid performance (def some areas of improvement, but a solid start). Im definitely in a better situation performance-wise than I would have been in if I would have gone with the loop idea.