So I'm trying to make a character that scans for the closest marker and then moves to it but I don't know why it isn't working.
game:WaitForChild("Workspace")
local markers = game.Workspace.Markers:GetChildren()
local hum = script.Parent.Humanoid
local humRoot = script.Parent.HumanoidRootPart
local function findClosestMarker()
local currentMarker = nil
local shortestMarker = math.huge
local chosenMarker = nil
for i, marker in pairs(markers) do
local distance = (marker.Position - humRoot.Position).magnitude
currentMarker = distance
if currentMarker <= shortestMarker then
shortestMarker = currentMarker
chosenMarker = marker
print("Found Closer Marker")
elseif currentMarker > shortestMarker then
print("Did Not Find Closer Marker")
end
end
return chosenMarker
end
hum:MoveTo(findClosestMarker().Position)
hum.MoveToFinished:Wait()
