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?
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
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.
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.
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.
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.
-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)
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)