I need help with this AOE effect

Greetings. So basically I was making a reversal red (an explosion type of technique btw) and when I use it the enemy npc flys to the direction, but only that direction. I want the npc to be able to go in all directions for example, I am behind the npc, I want it to go flying the opposite direction like it goes for flying far behing me if you get what Im saying heres a vid
robloxapp-20240403-0302575.wmv (4.2 MB)
Heres the script too

Hitbox.Touched:Connect(function(hit)
		if (debounce) then
			return
		end
		if hit.Parent ~= character and hit.Parent:FindFirstChild("Humanoid") then
			local echar = hit.Parent
			local ehum = echar:WaitForChild("Humanoid")
			local ehumrp = echar:WaitForChild("HumanoidRootPart")
			
			debounce = true
			task.wait(.85)
			
			ehum.Health = ehum.Health - 10
			debounce = false
			
			local dir = (ehumrp.Position - humrp.Position).Unit

			local bv = Instance.new("BodyVelocity",ehumrp)
			bv.MaxForce = humrp.CFrame.LookVector * 2000
			bv.Velocity = (dir + Vector3.new(0,1,1)).Unit * 100
			bv.P = 500
			game.Debris:AddItem(bv, .5)
			
			ehum.PlatformStand = true
			task.wait(2.5)
			ehum.PlatformStand = false
			
			Hitbox:Destroy()
		end
	end)
	
	wait(.8)
	local Effect = Explosion.Attachment.Spikes
	Effect:Emit(40)
	RocksModule.Ground(humrp.Position + Vector3.new(0,1,0), 17,Vector3.new(2,2.5,2),nil,20,false,1) 
	game.Debris:AddItem(Explosion,1)
	Red:Destroy()
	Hitbox:Destroy()

How do I change this and if anyone has experience with this, how do I make it better?

2 Likes

Get a unit vector from the players position to the enemys position

(enemyRootPart.Position - rootPart.Position).Unit

then set your body velocity velocity to that unit vector times by some force

local maxForce = 1000 -- configure this
local FORCE = 100 -- configure this too

bv.MaxForce = Vector3.new(maxForce, maxForce, maxForce)
bv.Velocity = (enemyRootPart.Position - rootPart.Position).Unit * FORCE
2 Likes

ok this solved that problem but i have another, I have a hitbox but the hitbox detects only one person, how do i make it detect multiple players? If you know ofc

1 Like

First question: I’m assuming the hitbox is just a part, what are the properties of the part regarding
canCollide, canQuery, canTouch. (I think if can collide is true you wont be able to see the other two but I’m not entirely sure.)

but the hitbox detects only one person, how do i make it detect multiple players?

I think its because of your debounce. Essentially:

  • Player casts the explosion
  • (debounce is currently false)
  • Enemy is hit and touched is triggered, so we fling them and set debounce to true
  • Another enemy is hit, but because debounce is already true, we break out of the code early.

Therefore, only one enemy can be hit.

To fix it, you could use a sort of tagging system. Essentially, every time a player is hit by the explosion, we add a temporary tag to that player. Then whenever a player is hit, we break out of the code (similar to your debounce) IF they already have a tag applied.
This is like each enemy player having their own unique debounce, as opposed to the explosion having its own global debounce.

However, I think this approach for the explosion may be better:

  • Explosion technique is cast (validate to see whether the player can cast [not on cooldown, etc])
  • Create a hitbox part, and correctly position and size it.
  • Get all the parts touching the hitbox
  • Filter the parts so that we only have enemy humanoid root parts
  • Run an explode function on those parts.

This method doesn’t rely on the touched event, I think this is better because the documentation says:


(I’ve had trouble in the past doing hitboxes with the touched event)

So some semi-pseudocode for my proposed method:

function explosionTechnique()
      -- this function should only run when a player has pressed the correct key AND has no cooldown or anything like that
      
      -- create and position the hitbox if you don't already have a reference to it
      
      local touchingParts = workspace:GetPartsInParts(Hitbox)
      local touchingHRPs = {}

      for _, part in touchingParts do
         local HRP = part.Parent:FindFirstChild("HumanoidRootPart")
         if not HRP then continue end

         if not table.find(touchingHRPs, HRP) then
            table.insert(touchingHRPS, HRP)
         end
      end

      for _, HRP in touchingHRPs do
            explode(HRP)
      end
end

function explode(part)
   -- this should create the body velocity and child it to the part parameter
do

Didn’t intend for this to be so long, sorry!
Also, I didn’t actually test the code, so if theres any issues (minor or major) please tell me.
Thanks!

1 Like

Dude u seem smart so ima re-read what you said a couple of times then use the code sorry I’m a bit slow :sweat_smile: but once i figure it out ill let u know and ill definitely use this code and study it

Also incase i couldn’t figure it out, could you combine it with my current code so I can see how you did it and how I should organize it