How would you detect a sword hitting an object?

I am scripting a sword for someone, and I would like to know the best way to detect when the sword hits hits an object. Should I raycast, or used a .Touched event?

2 Likes

I personally would use a .Touched event. However, others may suggest another method.

1 Like

Different developers will give you different answers and there really isn’t a best answer to this question. Just be wary of what your implementation needs and work around that. In either scenario though, just be wary about needing to guard against the client and the discrepancy between the server and the client in terms of how quickly they can process things.

I personally prefer raycasting in all scenarios but will use touched if I’m lazy.


This may not be fully related to your question but a developer has posted about raycasting-based hitboxes if you’re interested in having a look. It’s honestly really cool.

3 Likes

Should I do it on the client or the server?

Either way works. I personally keep this kind of logic to the server so long as the rest of my code is operating at peak efficiency. I hold this theory that if the rest of my code is optimised, there’ll be room for the server to run these operations without a huge burden placed on memory or anything.

As far as the client goes, you’re establishing a level of trust with them in being authoritative of where they’re hitting. The server is then responsible for confirming that a hit they performed is valid and should be passed.

1 Like

Hey, when your animation plays you can do one of these things:

  • When the mouse is clicked, check if the mouse’s target (PlayerMouse.Target) is at the desired object, and then check the distance from the player’s character and that object with Player:DistanceFromCharacter()
    Here’s some documentation: Player | Documentation - Roblox Creator Hub
  • Used the Touched event, but make sure that the sword is swinging. Then get if the object is the desired object, you get the drill. BasePart | Documentation - Roblox Creator Hub
  • Use raycasting to detect if anything intersects the path of the sword, while still using Ray’s Distance function.

You could probably do atleast a couple more checks, just to be completely secure, such as checking from the server and not the client, if that’s not already what you’re doing.

Happy coding!

2 Likes