How to get every npc inside a part

so im basically trying to make a laser and in which will kill every living thing inside of it, but the problem is that touch event doesn’t fire since the npc is already inside of the part

image
i want to kill all of these npc inside of this box

Well, you could find them like this:

local part = script.Parent --the part with all the NPCs inside of them

part.Touched:Connect(function(NPC)
    if NPC.Name == "Dummy" then
        NPC.Health = 0
    end
end)

it doesn’t trigger .Touched because they’re already inside and not beginning a touch

2 Likes

I’d work with BasePart:GetTouchingParts() to get a table of all of the parts, then filter through that to get all of the models/humanoids touching and then kill them all

you can use OverlapParams for this
https://developer.roblox.com/en-us/api-reference/datatype/OverlapParams

3 Likes

I would second Ayoub in using OverlapParams for this

2 Likes

sorry to bother but how would i filter through each part, like it works in all but it does multiple damage per part, lets say i set the damage to 10 and i wanna do 10 damage to each npc, but in each part of the npc it deals an additional 10, and the npc’s got 7 parts including the humanoid root part, so 7 * 10 and 70 damage taken and would be 30 health remaining for the npc’s
image
here’s the script

local part = script.Parent

local parts = workspace:GetPartsInPart(part)

for _, a in pairs(parts) do
	local model = a.Parent
	if model.ClassName == "Model" then
		local humanoid = model:FindFirstChild("Humanoid")
		if humanoid then
			print("hey")
			humanoid:TakeDamage(10)
		end
	end
end

try making a bool value and label it “Damaged” and set it to false, Like this local Damaged = false. Then whenever you damage the NPC’s true do an if then loop to see if the NPC’s have been damaged, if not then let it damage the NPC’s and set damaged to true.
so it should look something like this

local part = script.Parent
local Damaged = false

local parts = workspace:GetPartsInPart(part)

for _, a in pairs(parts) do
	local model = a.Parent
	if model.ClassName == "Model" then
		local humanoid = model:FindFirstChild("Humanoid")
		if humanoid then
			if Damaged == false then
				print("hey")
				humanoid:TakeDamage(10)
				Damaged = true
			else
				break --Just in case
			end
		end
	end
end

it only works with 1 npc

you may have to make separate scripts for each individual npc, where if they hit that part then they’ll take damage

Not the best idea. The best solution would be to have an empty table, and insert humanoids who have been hurt into it. Then before damaging, make sure the humanoid is not already in the table, which if it is, skip that iteration using the keyword continue

2 Likes

just a suggestion but you could use ZonePlus

it doesn’t work for npcs, only for players

you could use the part entered function, although you would have to use some more complicated things to check it being an NPC

yeah i could do that but the only problem is that

its probably the same for zone plus

1 Like