Trouble with GetTouchingParts()

Hello!

So i’ve ran into a issue related to the GetTouchingParts() function when i was coding a custom vehicle explosion effect with it.

The explosion is supposed to damage any Humanoid that is within the ExplosionPart with a tenth of the vehicle’s MaxHealth. it works but, since characters have multiple parts, and it detects them, it causes them to end up taking way too much damage than they actually should, and i can’t think of a way to prevent that from happening.

This the section of the script that manages the explosion and damage to humanoids:

local Parts = ExplosionPart:GetTouchingParts()
local Affected = {}
	for i, v in next, Parts do
		local Distance = (v.Position - ExplosionPart.Position).Magnitude
		local DistanceFactor = Distance / ExplosionPart.Size.Magnitude
					
		local Humanoid = v.Parent:FindFirstChildWhichIsA("Humanoid")
					
		DistanceFactor = 1 - DistanceFactor
		table.insert(Affected, v)
		if Humanoid and Humanoid.Parent:IsA("Model") then
			local RootPart = Humanoid.Parent:FindFirstChild("HumanoidRootPart")
						
			Humanoid:TakeDamage((Stats.Health.MaxHealth.Value / 10) * DistanceFactor)
			Humanoid.PlatformStand = true
			delay(.05, function()
				if RootPart and RootPart:IsA("BasePart") then
					local DirectionVector = (RootPart.CFrame.p - ExplosionPart.Position).Unit
					local Velocity = (DirectionVector * (tonumber(Stats.Main.ExplosionForce.Value)) * DistanceFactor)
					RootPart.Velocity = RootPart.Velocity + Velocity
				end
			end)
			delay(math.random(25, 30) / 10, function()
				Humanoid.PlatformStand = false
			end)
		end
		delay(2, function()
			table.remove(Affected, 1)
		end)
	end

Also, it seems that i cannot apply any velocity to the characters RootPart for some reason, as they are supposed to be pushed away from the explosion. Am i doing something wrong?

2 Likes

You’ll need some way of keeping track of which players have been hit.

Initialise a table to store this before the for loop (I recommend storing the humanoids of players who have been damaged)

local humanoidsHit = {}

Now, for every iteration of the loop where you find a humanoid, check if it has been damaged yet by iterating through the table. If they have, continue, if they haven’t - add them to the table and deal damage:


if Humanoid and Humanoid.Parent:IsA(“Model”) then

		local humanoidIsAlreadyDamaged = false
		for _, h in pairs(humanoidsHit) do
			if h == Humanoid then
				humanoidIsAlreadyDamaged = true
			end
		end
		if humanoidIsAlreadyDamaged then
			continue
		end
		table.insert(humanoidsHit, Humanoid) -- Log that the humanoid has been hit

… continue to damage the player

1 Like

Thank heavens! This stopped the continuous damage, but i still can’t get players to be pushed by the explosion force, but it’s better than nothing i suppose.

1 Like

Were they being pushed before you added my code? Or is that unrelated to the topic?

It already wasn’t functioning before the new humanoid table addition. I’m still trying to figure out why it isn’t. :frowning_face:

Nevermind. I got it working by using a BodyVelocity on the player’s character’s HumanoidRootPart instead of directly applying velocity on it.