Get distance from certain entity?

I’m currently working on a Drug Lab addon, and I’m trying to add coolers which need to be near the lab.
I need to find a way to get the closest Drug Lab Cooler (drug_lab_cooler entity name)

Here is my current code:


	local distance = self.Entity:GetPos():Distance( self:Getowning_ent():GetPos() )
	if (distance > 100) then
		local chance = math.random(1,5)
		if (chance == 2) then
			DarkRP.notify(self:Getowning_ent(), 0, 4, "Your drug lab is overheating!")
			self.burningup = true
			local burntime = math.random(8, 18)
			self:Ignite(burntime, 0)
			timer.Simple(burntime, function() self:Fireball() end)
		end
	end

At the moment, it just lights up if the player is too far away, how can I make it so instead of the playing being too far away the closest drug_lab_cooler is too far away? I haven’t done lua in a long time this is my first addon for like a year. I would really appreciate help, thank you! :slight_smile:

use ents.FindInSphere and then loop through the table returned by that and if the ent class matches “drug_lab_cooler” and the distance is lower than 100 disable overheating or whatever

You may want to use DistToSqr() instead of Distance():Length() as it is a lot more optimised.