Should I use math.min for this?

Hello I have a function that I want to simplify.

local function GetClosestShop()
	local Distance = 6
	local Closest = nil
	for Index,Shop in pairs(ShopData) do
		local newDistance = LocalPlayer:DistanceFromCharacter(FunctionParts[Index].Position)
		if newDistance <= Distance then
			Closest = Shop
		end
	end
	return Closest
end

I think I should use math.min.
Is that possible?
I took me some time to make this one but I’m happy to make it smaller.

2 Likes

Nah, this seems pretty solid. You’re going to need to loop through FunctionParts eventually, so you might as well check their distances while you’re iterating!

Also, math.min doesn’t give you an index, so you’d only be able to get the minimum distance from it, not the shop itself.

1 Like

You could use the math min function to filter out inputs that have positive numbers if ya dont want that.

1 Like