Math.Min got vector3

Im putting locations in a table then getting the smallest value with math.min but it prints this
image


local function Wander()
	local Nodes = workspace.Nodes:GetChildren()
	for i, v in pairs(Nodes) do
	table.insert(Distances, Vector3.new(v))
print(math.min(unpack(Distances)))
		
	end
end

It’s because you’re trying to sort a list of vectors by smallest which doesn’t work. You’d need to convert them into a single number, probably via .Magnitude if you want to get the distance. Otherwise could you explain what you want to achieve so I can be more specific?

i want to get all map locations so the npc can go through them

Could you be more specific? If you just want the locations why do you need the math.min function? If it’s based on how close the NPC is to the map location you could use .Magnitude instead, as suggested.

Ok i want to put all the location position in a table and then check the closet one to the player

use this function =

local t = {5, 5, 6, 4} -- example table

table.sort(t, function(a, b)
	return a < b
end)

print(t[1]) -- return "4"

Ok i tried this but how would i also put the location name?

	local distance = (Wendigo.HumanoidRootPart.Position - v.position).Magnitude
		table.insert(Distances, distance, v.Name)
		print(math.min(unpack(Distances)))

What do you mean by ‘location name’ and ‘where to put it’ ?

Idk im so confused rn what im basically trying too do at the end of the day is make a npc move to the closet location

There are a couple of options. You could make it into a dictionary e.g

{["locations"] = ["location1"] = 1, ["location2"] = 2} --etc.

Otherwise you could use a table.sort function which takes into account magnitude and retains the positional vector in the table at the same time.

table.sort(t, function(a, b)
        local magA = (Wendigo.HumanoidRootPart.Position - a).Magnitude
        local magB = (Wendigo.HumanoidRootPart.Position - b).Magnitude
	return magA < magB
end)
1 Like

Please tell me what are you storing inside your table. Are they Vector3s of CFrames or just parts ?

i wanted to first put vector3’s


local ClosestLocation = {}
local ClosestLocationName = {}

 for i, v in pairs(workspace.Nodes:GetChildren())
        local dist = (Wendigo.HumanoidRootPart.Position - v.Position).Magnitude

        if ClosestLocation[1] == nil then
           table.insert(ClosestLocation, dist)
           table.insert(ClosestLocationName, v.Name)
        end

        if dist > ClosestLocation[1] then
            table.remove(ClosestLocation, 1)
            table.remove(ClosestLocationName, 1)
            table.insert(ClosestLocation, dist)
            table.insert(ClosestLocationName, v.Name)
      end
end

Wendigo.Humanoid:MoveTo(game.Workspace.Nodes:FindFirstChild(ClosesLocationName[1]).Position)

So all what happens in this little snippet of code is that theres a for loop which will go through every node and then it will compare which magnitude is bigger and then at the end of the for loop you will have the closestlocation in the tables which you can then retrieve and use the MoveTo function inherited from the Humanoid to move the player to the closest location.

That seems like a very inefficient way of doing this:

local t = Nodes:GetChildren()

table.sort(t, function(a, b)
        local magA = (Wendigo.HumanoidRootPart.Position - a.Position).Magnitude
        local magB = (Wendigo.HumanoidRootPart.Position - b.Position).Magnitude
	return magA < magB
end)
1 Like

How is that very inefficient? You are only returning numbers but I am also making it easy to retrieve the name of the destination. As long as it works then it works unless you want to rely on performance which in this case it wouldn’t hinder much.

The function sorts the nodes themselves, so it would be pretty easy to access any property of said node. If he uses this repetitively performance becomes more of an issue. Although even if it isn’t, it’s always nice to have performant, simple code.

But you are returning the magnitudes back into the table to be arranged not the nodes…

The function returns true if the magnitude of A is less than the magnitude of B. Since A and B are passed as arguments they are the ones that are sorted based on this function. The returned value is either true or false, nothing else is passed back. This means that it sorts based on whether or not A is closer than B. In all honesty this could be achieved with even more efficiency using meta tables, but I didn’t think it was totally necessary for this use case.

The first few lines explain it pretty well:
https://www.lua.org/pil/19.3.html

1 Like