How to make this?

Hey guys. I’d like to know how I can make this only detect a model. Firstly, it only runs one time. Secondly, if I don’t add a max limit, depending on if a certain area has lots of parts in it, the game will crash. Any way to fix this? Big thanks if you know a way!

local Obj = script.Parent

local OverParams = OverlapParams.new()
	local FilteredInstances = {}
	OverParams.FilterType = Enum.RaycastFilterType.Exclude
	OverParams.FilterDescendantsInstances = FilteredInstances
	OverParams.MaxParts = 50
	
	local PartsInBox = workspace:GetPartBoundsInBox(Obj.CFrame, Obj.Size, OverParams)
	
	RunService.Heartbeat:Connect(function(deltaTime)
		for i, v in ipairs(PartsInBox) do
			local Humanoid: Humanoid = v.Parent:FindFirstChild("Humanoid")
			if not Humanoid and not table.find(FilteredInstances, v) then
				local OtherParts = v
				table.insert(FilteredInstances, OtherParts)
				print("Added to filter.")
			elseif Humanoid and Humanoid.Health > 0 then
				Humanoid.Health -= Damage
				print("Damaged!")
			end
		end
	end)
1 Like

I presume the reason the game crashes because you’re iterating through tons of parts without a limit every frame…

1 Like

This wasn’t exactly the issue, but thanks anyway I guess.