I’m currently working on a viewmodel gun system. Due to server latency, I’m spawning bullets locally on the client and using a RemoteEvent to send hit information to the server when a bullet hits a target. The server then handles the damage logic.
However, because the bullets are only created locally, other players can’t see them. I’m not sure how to properly replicate the bullets so they’re visible to other players while keeping the fast-paced movement accurate.
Try looking into RemoteEvent :FireClient() and .OnClientEvent().
Although, since the information you are sending is Visual, it might be better to instead use an UnreliableRemoteEvent for spawning the bullet on other’s screens.
The Server can loop through all the players (except the one who fired the bullet) and send them the necessary information to create their own visualization of the bullet. This is done via the :FireClient() method. Then, in a LocalScript, each Client can have an OnClientEvent() signal that receives the information.
If you don’t want to loop through all players, you can instead use :FireAllClients(), but you’d need to add an additional check in the LocalScript if you shot the bullet to make sure you aren’t visualizing the bullet twice.
Then from that point, it’s as simple as reusing the code used to make the original bullet visualization but from another’s Point of View.
Here is some additional documentation that may be helpful.
What I do is the shooter player sends a remote event to the server telling them they want to shoot at this position, the server verifies the remote event, then the server fires to all client, sending the position, and each client simulates the projectile and hitbox. Once a hitbox hit is detected, then only the shooter player should fire the remote event to handle damaging the target. You’d also verify this remote as well, so that it’s only the shooter player that is able to say whether the bullet should damage the target.
You could give the client network ownership of the bullet from the server. Be VERY careful with this though as you don’t want to be able to give the client network ownership of anything other than the bullet so do many many checks to make sure.
Make the bullets on the server side instead. What I do for gun systems is just doing it all in server scripts and calling a local script when needed for animations and such.
Making the Bullets on the Server is Bad Practice, well it depends, if it’s from something that’s rapid fire (Pistols, Machine Guns, etc), then it’s Best to make it in the Client, As Visuals, But if it’s something that needs to be very precise, like for example a Battleship’s Cannon (especially if the projectile is slow and has bullet drop), then do it on the Server
Yes, I initially considered creating bullets on the server as well.
However, doing so introduced latency between the server and the player, and as Bartholomew mentioned, since my gun has a high rate of fire, this approach doesn’t seem suitable for my weapon.
If you make the Projectile in the Server, let the server handle it, if you set the NetworkOwnership to the client, it may end up like Criminality… basically exploiters making guided missiles from the RPGs (since the rockets is made in the server, and the NetworkOwner set to the client)
In Summary, In my Opinion best approach is
Only use Client Bullets for Projectiles from small arms fire, basically infantry weapons or bullets that tends to appear alot
Use Server Sided Bullets for Slower Projectiles that needs maximum precision in all clients, like from a Cannon, or a heavy projectile from something like an artillery
Yes, I’ve also heard from people around me about exploits related to Criminality. Due to its potential drawbacks, using NetworkOwnership is something I’ll need to carefully consider. Thanks for the advice.
So since you already mentioned how bullets are created locally (and I assume this means the visualization as well), you can reuse that code.
If you haven’t made any visualization at all, then that’s another thing, but if you already have a way for the Owner to see their own bullet, then it’s as simple as copy pasting the code over or putting it into a ModuleScript or Function for multiple kinds of uses.
event.OnClientEvent:connect(function(position,direction)
--[[ Run your visualization code, but instead use the Position/Direction received from the RemoteEvent
so it happens from another player's position instead of your own--]]
end)
OnClientEvent will be triggered inside a LocalScript when a ServerScript fires a RemoteEvent with either FireClient(player) or FireAllClients().
that’s just the nature of the client-server architecture. In order for all clients to see the bullet, they need the information to render it locally, and you have to do that by sending the mouse pos from the shooter to the server so the server can send it to all clients. There’s no workaround around making the bullet visible to others…unless u did the bullet on the server, which would be laggy
I haven’t had experience with any FastCast Module, so I don’t exactly know how you would visualize a bullet with it.
However, for the recalculation part, you don’t necessarily need to calculate a new trajectory as long as the person who Shot the bullet sends their trajectory information to the Server, and then the Server sends the trajectory information to every other player.
You can create the bullet on the server but still have it look smooth for the player who shot it by setting its network owner to the client.
Edit: Nevermind what i said, since the bullet isnt an actual physics object you should fire a remote event to all clients with information on the bullets trajectory for them to recreate themselves.
Since you’re using fastcast, run whichever function that created the bullet, in the client instead of the server… it won’t really matter… except it obviously won’t damage, so you have to use remote events for the damage, and create the bullet in all other clients WITH the same Origin and Direction, so it Replicates to everyone