Making it so explosions dont kill through walls

Hello, so im making a explosion and i want it to kill people when they are in the radius, but i dont want them to die if they are inside a building and say the explosion is outside of the building and it still kills them. I tried raycasting from the players head to the explosion position to see if theres a part obstructing it, but its was very inaccurate, heres the code for that.

Also this is linked to a c4 script im currently making. The raycasting doesn’t seem to work. Can someone help me with this, Thanks!

2 Likes

Sorry about the late Reply! Better late than never.

Check for every body part instead of just the head and ignore the HumanoidRootPart. Why ignore it? Some crouching systems are weird and don’t work correctly. This just helps with the hassle.

1 Like

Seems like your Ray.new call is incorrect. The second parameter should NOT be where you want the ray to go, it should be the direction to where you want it to go. Try

Ray.new(v.Head.Position, (Current.Position - v.Head.Position))

use workspace:GetPartBoundsInRadius to get all the players inside of the explosion, loop through them, and do a raycast check similar to what @ThanksRoBama suggested

that way you dont have to loop through every humanoid
also Ray.new() is deprecated and I suggest using workspace:Raycast()

It would be better to utilize the Explosion.Hit event instead of looping through workspace. Then from that, you can do the raycasting code.

1 Like