Server side hit detection

Hi there, I’m working on a gun system, but I’m having some problems lately, I have made the bullet replication/visuals on the client side, together with hit detection, the problem is that having hit detection on the client side sometimes kills another player on your screen, but wont kill them on there screen, also its very unsecure to exploits to have the hit detection client sided, I tried to make the hit detection on the server side, but any server script which I parent to the bullet/projectile wont work because I’m cloning the bullet from a client script (I’m cloning the bullet from a client script to avoid lag)

How would I go about making a server-sided hit detection?

Any help is much appreciated! :smile: :+1:

Are you using raycasts or checking if the projectile touches anything? For raycasts, you might want the client to send information about the raycast to the server, like what direction they’re pointing in. If you’re checking if the projectile touches anything, you’re going to need to make the projectile server side, otherwise the server can’t work with it.

1 Like

Coincidentally, I just finished making my own hit detection system! There’s more to this than it seems at first, but you basically have two branches of options here:

Do hit casting on the server as well as client (easy way)

Have the client fire a remote event whenever it fires, and when the server receives the event, run through anti-exploit checks (such as fire rate, possible at all, etc), and do the cast on the server, replicating it and the hit detection to the other clients, effectively making the client cast visual only. (You can use a remote function to return to the client what parts you want it to make transparent/delete so it doesn’t see the server-sided visuals)

Lag compensation (hard way)

There are many ways you can make lag compensation, but they’re all significantly harder or than the previous method. Counter-Strike, a very popular competitive FPS multiplayer game, does this by saving player positions frequently, and rolling back their position to when the bullet was actually fired (compensating for latency, or ping). Here is a short article on their website that explains it some more: Lag Compensation - Valve Developer Community
Keep in mind that this isn’t the only way to do lag compensation, and with a bit of creativity, you might find one that works best for you! The project I’m working on right now absolutely needed lag compensation due to high-velocity movement, as with the first method, bullets would show up way behind where you fired them (due to latency). If you do go ahead with this method, I suggest using the PlayerPing module, which finds player pings to terrifying accuracy (I’ve used it for my implementation).

2 Likes

I’m using BodyVelocity and checking if it touches anything.

I’m asking how to do that.

Be warned, this is a really inefficient method of projectile physics for a game. I really reccomend using FastCast, I use it personally for my current project and it works amazingly, handling thousands of bullets while the BodyVelocity method could only handle a few (and glitchy at that!), plus comes with its own built-in hit detection system and pretty much anything you could want to relate to bullet physics. It’s also very easy to use and set up, and even comes with a sample gun that you can just use the code of. By default, FastCast is server-sided and handles everything on the server, but you can make it client sided and add lag compensation as I just finished doing, but this requires a lot more programming than it looks like.

1 Like

Well, to do it server side, I recommend taking the code you use for the projectile and projectile spawning, and put it on the server, and when a player fires, send the information relevant to the firing like look direction or anything else you use to the server using a remote event. That way the server handles all the projectiles to prevent cheating and the results are replicated to all clients.

I have already compensated for lag by making the bullets replicate on the client side then telling the server to copy the bullet replication to all other clients, but now I have a problem with server-side hit detection…

I already tried that, but because I’m replicating the bullet on the client side, any server script parented to the bullet doesnt work for some reason, even when I fire a remote to the server.

That would cause unplayable lag.

I’m replicating the bullet on the client and then telling the server to copy that to all other clients to avoid lag, but now I have a problem with server side hit detection

I wouldn’t put a script on each bullet, that could cause a lot of lag if you have a lot of bullets at once. Instead, have one script that manages all bullets, either using an array or Collection Service. The centralized script would make it easier to code around, as everything relevant to bullets goes to one script instead of finding the corresponding bullet.

1 Like

Again, FastCast would probably solve this issue, but to solve it with your physics simulation approach, you’d want a duplicate bullet on the server which has the script attached to it. The server can’t parent a script to your client-sided bullet because the bullet does not exist on the server.

Could you link some topics/DevHub post or explain how I could do that?

Sure,

Arrays/Lists/Tables/Dictionaries: Tables | Roblox Creator Documentation

Collection Service: CollectionService | Roblox Creator Documentation

CollectionService functions the exact same as putting scripts in each of the objects afaik. Using an array could definitely be more performant, but I’d worry more about the physics. Roblox (and most game engines for that matter) can’t handle many physics parts being active at once, especially not for many bullets colliding everywhere.

The script is already in the part that gets cloned.

Yes, but cloning it on the client won’t replicate the script as all server scripts aren’t visible to the client.

Sure, personally I would just use raycasts, there are times when projectiles are the way to go, like cannon balls or rockets. I’m giving Unbound the benefit of the doubt and trying to work with projectiles.

I see, but I have no idea how to use FastCast.

Again, I think FastCast is the best solution here, as it gives full projectile behavior while maintaining the vastly superior performance of raycast-based systems.

It’s actually pretty simple, the creator provided a sample / debug gun along with the module which has everything you’d need in it, all already implemented for you. Basically no knowledge needed. I’ve made my own guns in minutes using the scripts inside of it, and you can tweak that script however you want to achieve your desired gun.

@Monkepath gave you a clear answer. I’ve just finished working with an FPS framework too, I used FastCast on both client-side (just for the projectile visualization) and server-side (actual hit detection). FastCast actually handles lag (FPS lag not ping) quite well using the delta time, so even if the server and client have different FPS it still works fine.