How to add all damage to a Humanoid depends on what part was hit

So i have an example where the blast explosion both touch the Head and the Torso. I want to add both damage that was hit by the blast where Head = 75 and torso = 20. The Problem is it only prioritize the first condition and ignore the rest. How can I add or get the total damage if blast touch both head and Torso?

local BlastPart = script.Parent
BlastPart.Touched:Connect(function(hit)
	if hit.Name == "Head" then
		hit.Parent:FindFirstChild("Humanoid").Health:TakeDamage(70)
	elseif hit.Name == "Torso" then
		hit.Parent:FindFirstChild("Humanoid").Health:TakeDamage(20)
	end
end)

1 Like

Not exactly sure what the problem is. This script can hit the torso or head multiple times. You might want to use like get bounds in box or get parts in region3… Not sure what you mean is happening wrong tho…

i will add some debounce later if i figure out how can i total the damage if blast hit both torso and head.

Are you sure the character has a “torso” and not an upper and lower torso? Other than that idk why it wouldn’t do both unless you’re not touching the torso.

1 Like

You can create a variable called touched and assign its initial value to false and then once it has hit someone, set it to true. Also, you should make sure a humanoid object exists before trying to modify it because, if one isn’t found, FindFirstChild returns nil.

local BlastPart = script.Parent
local touched = false

BlastPart.Touched:Connect(function(hit)
	local humanoid = hit.Parent and hit.Parent:FindFirstChild('Humanoid')
	
	if not touched and humanoid then
		if hit.Name == "Head" then
			humanoid.Health:TakeDamage(70)
			touched = true
		elseif hit.Name == "Torso" then
			humanoid.Health:TakeDamage(20)
			touched = true
		end
	end
end)
2 Likes

Might wanna use GetPartBoundsInRadius to get all of the parts inside a sphere.

1 Like

I would do:

local BlastPart = script.Parent

local damages = {["Head"] = 75, ["Torso"] = 25}

local parts = workspace:GetPartsInPart(BlastPart)

for _, hit in ipairs(parts) do
   local hitName = hit.Name
    local damage = damages[hitName]
   
    if damage then
        local humanoid = hit:FindFirstAncestorOfClass("Model")

        if humanoid then
           humanoid:TakeDamage(damage)
        end
    end
end
4 Likes

i tried it using your code and it works m friend. but i change the “FindFirstAncestorOfClass” into a “FindFirstChild” . Thank you

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.