✓ Accepted answer
There are two ways that I know which you can use in order to do this:
- Creating the projectile on the server and using network ownership to move it
In order to do that using this way, you would create the projectile on the server, set its ownership to the player and let the player's localscript handle moving it.
I personally don't have any experience with it, but I believe this way would only be good for slow projectiles such as rocket launchers, due to the delay in communicating between the server and the client.
And that's not the only issue with it. As you can read on this page, you cannot apply network ownership to anchored parts, meaning your bullet would have to be unanchored, therefor forcing you to use BodyMovers, which may not always be preferred.
- Creating the projectile on every client separately
This way let's you anchor the projectile, and move it by setting its CFrame, without the need to use BodyMovers. In order to do this, you would have the server tell every client to create the projectile here and there, moving this way or that way.
This way is probably better for fast bullets. I know for a fact that Jailbreak uses this way in their weapons system.
TL;DR in my opinion, it's better to create the projectile on the client, especially for fast guns.
7 comments
What would be the best way to set it's CFrame, tweenservice or lerping or? (If I wanted an anchored projectile to move forwards at a constant speed until it hits something)
If you use TweenService, you will have to calculate the "time" parameter of it. Otherwise the bullet would take the same amount of time to move over different distances, which means it would move very slowly on short distances, and very fast on large ones. Now, if you use a for loop without lerping, you can simply abort it after the bullet hits something or reaches the end of a ray.
If you do use lerping however, you will have a similar issue like with the TweenService.
Ah ok. One more question, if I am going to create a projectile on all clients, should I also make an invisible one on the server so I can see where it hits?
You don't have to. If your projectile is a fast one, like the ones on Jailbreak, you can just fire the RemoteEvent with the Ray, and have the server script check if the projectile hits a player (using the ray), and if it does, deal the damage to said player (and obv tell all clients to create the projectile).
My projectiles will be relatively slow compared to fast shooting ones in other games, so I want the effect to only begin once it's actually hit instead of instant raycasting, would making the actual projectile or calculating the distance and applying effects after a certain time be best?
Just realised if I calculate the distance and wait that'd be silly since if it was aiming at a player they would've moved. I don't want to strain the server with calculating projectiles itself though