How can I improve my zombie detection system?

I decided to use Region3’s for detecting when a zombie is inside of my range area.

local Experience = game

function CreateRegion3FromPart(Part)
	return Region3.new(Part.Position-(Part.Size/2),Part.Position+(Part.Size/2))
end

function GetEnemysInPart(Part)
	local Region = CreateRegion3FromPart(Part)
	local PartsInRegion = Experience:GetService("Workspace"):FindPartsInRegion3(Region,nil,math.huge)

	for _, Part in pairs(PartsInRegion) do
		local Enemy = Part.Parent:FindFirstChildOfClass("Humanoid")
		if Enemy then
			if Enemy:GetAttribute("IsZombie") then
				print("Enemy Found!".. Enemy.Name) -- Will work from here later.
				break
			end
		end
	end
end

while task.wait(0.5) do
	GetEnemysInPart(script.Parent:WaitForChild("TowerRange"))
end

Is there anything to change to make sure it will always detect a zombie?

Thanks, WE.

1 Like

Changing your entire approach to iterate through zombies and find the area they’re in instead of iterating through parts in an area to find zombies could be hugely helpful to performance. You can use CollectionService tags to get all the zombies, and store this data, updating it sometimes, and then work off that stored data so you don’t need to find all the zombies’ areas so often…

1 Like