Im putting locations in a table then getting the smallest value with math.min but it prints this
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?
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.
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)
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)
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.
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.