Improving client-server gunfire delay?

i am working on my first fps game and it is going pretty well, but when i was testing it out with my friends it was really difficult to hit eachother, even at really close distances and it was really annoying. (maybe it was a skill issue but i doubt)

currently my script works like this:
–runservice loop checks if the player is holding down the left mouse button
–the local script fires a remote event with the Viewmodel LookVector as a parameter (the direction)
–server script sends a bullet from the players head position in the viewmodel direction (im using FastCast for the calculations)

here are some videos and honestly, it doesnt look that bad, but is there any way i could make it even better?


2 Likes

@TFlanigan You can use BodyVelocity to push the bullet faster, BodyVelocity is the best way to fire a bullet

1 Like

For the Raycasting, its best to do it on the Characters PrimaryPart (aka the Head) if you are in first person, it will look like you are shooting at the center of your screen, this is a good thing, that way you have better accuracy,it is also a common method for many game developers as raycasting from the Muzzle of the gun doesn’t work at close range, even outside of roblox, its also better that you use a RemoteEvent to send Gun effects to All Clients.

Handle your Weapons on the Client, but for Calculations and Shooting, Handle then on the Server

3 Likes

If you’re using RemoteEvents to tell all clients to fire a bullet from your gun, you should instead immediatly call the function binded to OnClientEvent so it instantly shoots your own gun so it appears there’s no delay. Then for other players there will also be no visible delay because your character animation and the bullet reach their router at the same time.

1 Like

most fps games (pf, arsenal, etc) simulate each bullet on the client that fired it, then sends an event to call the hit (which is why you see people flying in the sky and spam killing people with an intervention or a knife)

this is a big drawback but it allows immediate hit detection

here’s an example of an fps engine I made

hit detection with 0 ping

hit detection with 1000 ping

in the example with 1000 ping notice how stuff like glass breaking and damage being dealt happen a while after the hitmarker sound

the bullet is simulated on the client and then hits are called on the client.

2 Likes

Make sure you are not using task.spawn. I was stupid enough to use it and not understand why my bullets are flying behind me. :smile:

And why not to make so if you are the shooter, you create your own bullet on client.
Other players get a client event to create bullet from your weapon locally.

1 Like

so basically just do the shooting part on the client and then fire a remote to deal the damage etc. ?

2 Likes

exactly, the way mine works is

-client shoots
–deal with clientsided firing logic (gun effects, shell ejection, etc)
–sends a message to the server (with a client-chosen bullet key)

-server recieves a shot
–deal with serverside firing logic (caching client-chosen bullet key)
–sends a message to all clients (except the one that fired the bullet) to replicate the bullet

-client hits their shot
–deal with clientsided hit logic (humanoid hit detection, bullet hole creation, particle —effects)
send a message to the server with the client key and result of the shot

-server gets the hit remote
–get the bullet’s stats from the client key (stuff like what gun fired the bullet, or a snapshot of the player’s stats)
–deal with serversided hit logic (deal damage, break windows)

2 Likes

i might do that, but what is the bullet key? is it like some sort of an anti cheat system?

1 Like

it’s simply a randomly generated string created by the client (use HTTPService:GenerateGuid())

and it’s used for storing what gun fired what bullet, or how much damage the bullet is expected to do, so on and so forth

if I just grabbed the gun the player is currently holding for every bullet hit then players can learn to quickswap from a shotgun to a sniper rifle to trick the server into thinking all the shotgun shots were fired from the sniper

(edit: and saving the shot data on the server is a lot better than giving the client control over it and sending it along with the hit detection lol)

1 Like

but how do i safely send the bullet info to the server if the bullet is being shot on the client?

1 Like

you have the server grab the bullet info from the gun the player is currently using (at the time of shooting the gun)

(disclaimer the system im talking about is for projectile weapons, hitscan weapons are way simpler)

-client shoots gun
–performs client shoot logic
–raycast the bullet
–perform client bullet logic
–send raycast result to server

-server gets shoot request
–performs server shoot logic
–sends to all clients to replicate bullet
–deal damage and server bullet logic

1 Like

how would that work for if the bullet was not an instance? (i mean just rays)

you mean hitscan? or something like FastCast?

yeah im using fastcast for my game