Handling projectiles efficiently in the server

I am trying to make a gun system that uses projectiles in roblox studio.

My current solution is to raycast from the shooter in the look vector direction, and if the raycast hit, save the position of the intersection. The code then moves the projectile until it reaches the point of intersection, and checks if there is an object there. If that object is a player it applies damage. If there is not an object it loops back to the start and casts another ray.

The issue I am encountering is that while the projectile is in transit, a player could move towards the shooter. The code would only run when the projectile reaches where the player originally was, but since the player moved, it would not register even when they are in the path of the projectile

I feel like I may be overthinking this, so It would be great if I could have some guidance on modifying my code or starting from scratch on a script for handling many projectiles on the server.
Thanks!

I have a method.

So, you get an offset vector, and you tween the projectile there. This offset is relative to the mouse’s target (or wherever the projectile is going to hit). From there, use Lerp to interpolate towards the goal.

How would I then detect if it is touching a part or a player, the touched event is a little buggy, and I am not sure if raycasting every frame is very effective.

Once it’s done interpolating to the position, of course.

Firstly, let’s establish that we handle the logic on server and the visuals on client.

On the server, we want to do simulate a projectile by raycasting in increments. What I mean is that for every frame (do this in a Heartbeat event), we raycast up to the number of studs the projectile moves in 1/60th of a second from the origin, then we establish a new origin at the furthest point the raycast reaches, and then we repeat for the next frame. We stop when we hit a target.

For the visuals, we fire a remote event to the client in order to signal to the client to create a projectile, with a unique ID, and move it. If, on the server, we hit a target, we fire another remote to the client in order to signal to the client to destroy the projectile (and create the hit effect).

I’ve done this before, but it isn’t very performant. I’d assume only one heartbeat connection is made and they all update, which in that case I have not done.

If the projectiles are assumed to be very fast, I’d recommend making them have one cast.