How can I apply a force to a part similarly to the Explosion class?

I would like for my game’s weapons to be able to apply forces to unanchored parts and enemy characters—I don’t want players of the same team to be able to push each other because it would certainly result in griefing. My current approach for this problem involves checking if the player hit by a weapon is an enemy, then making an explosion on the hit player’s client if true. This makes it so only enemy players get pushed around when hit but has the drawback of making weapons unable to move normal parts. This isn’t what I want, so I’d like to use a different method. Is there a way for me to replicate the way parts get moved by an Explosion? Ideally I would loop through the parts hit, check if they don’t belong to a teammate, and then apply a force proportional to the distance between the part and the origin of the fake explosion.

Ok here is my thought process for this.

server side

  • Create a region3 at the explosion origin. Use an explosion for cosmetics and the region3 for hit detection
  • Get the parts in the region3
  • if the part is a BasePart and not a descendant of the player, then apply an impulse to it using ApplyImpulse
  • if an enemy player is hit with the explosion. Fire their client with the impulse direction and magnitude.

The impulse to the player has to be applied on their end unless you want to change network owners which can get pretty messy.

The direction of movement for the part will then have to be calculated as

local direction = part.CFrame:VectorToObjectSpace(part.Position - originPosition)
--not too sure if the VectorToObjectSpace is needed. Not too familiar with assemblies

Then apply an impulse to the part as the direction*explosionMagnitude. The explosionMagnitude can be a value of your choice and it will take some tweaking to figure out but start with 5000.

This is just an idea on how to do it and I haven’t tested it, but I’m sure you will figure out a way to get it working.

3 Likes

Region3 is deprecated, so I suggest you use something like GetPartBoundsInRadius, since explosions are spherical.

Never heard of region3 getting deprecated could I get a link to this? also I do agree with GetPartBoundsInRadius, I overlooked it.

1 Like

I’ll try this method out soon and get back to you.

I’m already using that actually!

I ended up using a modified version of this concept. Thanks!