How do i make it so every humanoid gets damaged with my explosion?

Hey everyone

How would i achieve it so when my explosion VFX happends it damages all humanoids in it.
Instead of 1?

my damage code

if hit and hit.Parent:WaitForChild("Humanoid") and hit.Parent ~= character then
				local enemy = hit.Parent.Humanoid
				if enemy.Health > 0 then
					enemy:TakeDamage(damage)
     end
end

I’ve tried this, but it don’t seem to work. I’m sure i’m just doing it wrong

for _, Target in pairs(hit:GetDescendants()) do
				local humanoid = Target.Humanoid
				print("humanoid found")
			end

Also tried this

for _, Target in pairs(hit:GetChildren()) do
if Target:FindFirstChild("Humanoid") then
      local humanoid = Target.Humanoid
   end
end

now this happends.

Thank you for reading

for _, Target in pairs(hit:Getdescendants()) do

GetDescendants()

oh, yeh i had that, just typed it quickly here

edit: fixed it

Still two pairs of parentheses.

oops, sorry i just woke up like 5 min ago

local players = game:GetService("Players")
local debounce = false

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				v.Touched:Connect(function(hit)
					if hit.Name == "" then --change to name of blast part
						if debounce then
							return
						end
						debounce = true
						humanoid:TakeDamage(-10)
						task.wait(5) --prevent taking damage too often
						debounce = false
					end
				end)
			end
		end
	end)
end)

Local script, place inside StarterPlayerScripts folder or StarterCharacterScripts folder. Make sure to modify some of the references.

Are you sure damage from a local script would replicate to the server?

https://developer.roblox.com/en-us/api-reference/property/Humanoid/Health

You’re right, I’ll change it up slightly.

Instead of touched event you can use Region3, for this @EgoMoose’s Rotated Region 3 module might help a lot. You can use it to get parts within the sphere and then check if any parts of a player are within the sphere.

– LOCAL SCRIPT –

local players = game:GetService("Players")
local debounce = false
local repStorage = game:GetService("ReplicatedStorage")
local dmgRemote = repStorage:WaitForChild("DamageRemote")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for i, v in pairs(character:GetChildren()) do
			if v:IsA("Part") or v:IsA("MeshPart") then
				v.Touched:Connect(function(hit)
					if hit.Name == "" then --change to name of blast part
						if debounce then
							return
						end
						debounce = true
						dmgRemote:FireServer()
						task.wait(5) --prevent taking damage too often
						debounce = false
					end
				end)
			end
		end
	end)
end)

– SERVER SCRIPT –

local repStorage = game:GetService("ReplicatedStorage")
local dmgRemote = repStorage:WaitForChild("DamageRemote")

dmgRemote.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	humanoid:TakeDamage(10)
end)

Organisation:

image

So im trying Region3, but can’t seem to prevent it from hitting a million times.
It does hit mulltiple enemies now.
I know why it hits a million times, i just don’t know how to fix it.

while true do
		wait()
		local partsInRegion = workspace:FindPartsInRegion3(region, character, math.huge)
		for i, part in pairs(partsInRegion) do
				if part.Parent:FindFirstChild("Humanoid") then
					local char = part.Parent
					char.Humanoid:TakeDamage(damage)
				end
			end
		end

It is because this loop runs extremely fast.
Maybe use a Table, but i’m not 100% sure how i can use it here.

You need to check if that Humanoid was damaged before considering characters have multiple parts. You can try making a table and adding the humanoid to that table. And then you can make the script check if the humanoid is in that table.

local Damaged = {}
while true do
		wait()
		local partsInRegion = workspace:FindPartsInRegion3(region, character, math.huge)
		for i, part in pairs(partsInRegion) do
				if part.Parent:FindFirstChild("Humanoid") and if not Damaged[char.Humanoid] == true then
                    Damaged[char.Humanoid] = true --//You can also use Damaged[char.Humanoid] = nil to remove the humanoid from the list.
					local char = part.Parent
					char.Humanoid:TakeDamage(damage)
				end
			end
		end
1 Like

Thank you, that works. It was easier than i thought

1 Like