I spent the last year and a half figuring out how to make good hitboxes. Almost all Roblox games have mediocre hitboxes that need improvements, however I can cut them some slack because hitboxes are the most challenging networking system to get right.
Why?
Ping. <— I hate this guy
Ping is the round trip between the client → server → client. Because of this, the more ping you have the more delayed your hits are.
Ping is SO annoying and you can't get rid of it.
BUT FEAR NOT! There are solutions!
0.5Use Chrono
This replaces Roblox's replication and improves it a LOT. It also adds some extremely basic exploiter protection.
1Rollbacks / Snapshotting
This is the most important feature to add to your hitboxes. When a client hits a player, then the request reaches the server, the server would see something entirely different than what the client did. To fix this we have to go back in time to when the hit was made.
We will use workspace:GetServerTimeNow()
On the server it will constantly save every entity's positions in a table.
The table will have everyone's CFrame, LinearVelocity and
AngularVelocity (I will explain this later), Timestamp, any important
Entity states like IFrames (Invincibility frames), and latency at that time.
Do not use AssemblyLinearVelocity or AssemblyAngularVelocity.
Instead, calculate velocity using the difference between two snapshots.
Linear velocity
(Snapshot2.Position - Snapshot1.Position) / DeltaTime
Angular velocity
For rotation, we can't simply subtract two CFrames because rotations aren't ordinary numbers. Instead, we calculate the relative rotation between the two snapshots, extract the axis and angle of rotation, and divide the angle by DeltaTime. This gives us the angular velocity, which we can use to predict how much an entity will rotate. Remember, both of these calculations give us the average velocity between two snapshots.
Snapshots should be cleaned up after:
CurrentTimestamp - Snapshot.Timestamp >= 0.8
0.8 is your MaxRewind time. If a client sends a Timestamp
(CurrentTimestamp - Timestamp) that is greater than the
MaxRewind then reject it. Also, if the Timestamp is greater than the
CurrentTimestamp then it's in the future so reject.
BUT WAIT! If you are not using Chrono (for whatever reason) then you need to use an unreliable remote event to add new snapshots. Roblox replicates player's positions every 20hz, this is good for casual games but for combat games you need at least 30 hz to 40hz (Maybe even 60hz if you're crazy). Keep in mind that the more data and more frequent you push to the server the more bandwidth you use.
PLEASE PLEASE PLEASE SANITIZE WHATEVER CLIENT SENDS PLEASE DON'T TRUST IT.
Do NOT treat Player:GetNetworkPing() as the exact
client-to-server latency. It returns round-trip latency, not
one-way latency. Instead, you can estimate how old a client timestamp is
using workspace:GetServerTimeNow(). Keep in mind that this is
an estimate, and the server must validate any timestamp received from a client.
When a hit is made the client will send the current timestamp and who it hit to the server. On the server, it will take that timestamp and go back in time (to the closest snapshot) to see if the hit was possible. The validation hitbox should be bigger than the actual hitbox and scale up depending on the attacker's ping. But make sure you cap it, otherwise it could get insanely big.
2Velocity Interpolation & Extrapolation
Snapshotting has an issue though. When an entity is traveling extremely fast sometimes the snapshots can't keep up, and sometimes a snapshot has not been captured at that exact time. So the snapshot lags behind where the player is. To fix this we use algebra!
Remember the velocity we saved in our snapshots? That's what we will use. Remember in physics 101 the formula for distance?
Distance = Velocity * Time
With time being latency:
CurrentTimestamp - Timestamp
If you're using rollbacks, calculate the difference between your target simulation timestamp and the timestamp of your available snapshot. This tells you how far you need to extrapolate.
This predicts how far an entity has moved between the timestamp of our available snapshot and the target simulation timestamp. Once we have the distance we will offset the validation hitbox with that distance. Position AND rotation.
Another note is that this is a prediction, it is not guaranteed.
3Windows
This is more of a security measure. This tells the server how early, how long, and how late to accept a hit. This is self explanatory.
The server will call something like Hitbox:Arm() which will
enable the window. However, because of networking jitter sometimes
the hit request can reach the server before the server arms the hitbox. So,
when the server receives the hit request you have to yield slightly until
the window is enabled (or armed).
4Client-sided prediction
This is more of a core game design feature than a hitbox issue, but it helps with responsiveness a lot.
Instead of waiting for the server to give you the ok you immediately let the client continue whatever it was doing. However, if the server decides to reject it then the client has to clean up whatever it did.
You use a cleaner module for this.
Example
Client
- [Client Input]
- Plays move
- Hitbox Activates
- [Hits Entity]
- Plays VFX, SFX, etc.
Server
- [Client Input]
- Validates
- If validations fail then ask the client to stop what it's doing
- [Hits Entity]
- Validates
- If success then deal damage to the victim.
So yeah, that's all the knowledge I know about hitboxes!
If you get all of these right then congratulations! You made an excellent hitbox module.
Have questions? Don't hesitate to reach out! I don't bite! Much…
See it in action
The combat system in Bloodthorne, running in Studio.