Hello, I’m working on a sword attack system and I’m not sure where I should handle the animation and damage logic.
I have a sword animation (rbxassetid://96350089317444) that has a hit moment triggered by an animation event. If I run the animation on the server, it can easily handle damaging, but that can cause lag to the server and is not optimized. But if I run the animation on the client, it’s smoother and more optimized but it can cause desync if the player who’s attacking is lagging. I’d like some help on how to build a sword attack system with a syncing dmg and animation to all players
You can find an animator module that can work with the time system: workspace:GetServerTimeNow.
If such a module doesn’t exist, you can replace os.clock() in any animator module with workspace:GetServerTimeNow(). Also, add a new startTime argument to the :Play() method.
Next, you can replace the RemoteEvent with a RemoteFunction, since we need to get the server time at the moment of the attack.
Now we can invoke the RemoteFunction from the client, and it will return the server time. Then we can play the animation using our module.
Play the animation on the client so it looks smooth, but handle the damage on the server so it’s secure. Use the animation event to tell the server when the hit happens, then the server can check range and apply damage.
You can use workspace:GetServerTimeNow() if you want to sync timing, but it’s not always needed. And yeah, RemoteFunction is heavier than RemoteEvent, so it’s better to use a normal RemoteEvent for attacks since it doesn’t wait for a response and won’t cause delay.
The client should be playing the animation and sending a damage request to the server since immediate feedback is important for gameplay.
In that RemoteEvent, simply send the player to be damaged and have the server do a sanity check on whether that player is close enough. This alone should deal with any issues like lag since the server checks where the players are on the server and uses that to get the distance between them.
That’s the basic functionality though. If you want to account for things like ping, whether the attacker is looking at the player, etc, then that’s a different thing. If you’re looking for a resource to help with your system , I recommend [Shapecast Hitbox](ShapecastHitbox: For all your melee needs! [V.0.2.5]
since my attacking system is based on collision of the sword, only the client can actually hit enemies since only it can see the swing animation, or is there a better attacking system?