If I would like to find the nearest part to a player (with no min distance) how could I achieve this? I dont want there to be like a certain distance just the closest part
Have two variables.
- The best distance
- The best part
Then you can go through all of the children in the workspace and check if they are less than the best distance and if they are they are the new best part, and their distance is the new best distance. If you do not want a starting best distance, you can use nil and check if there is a best distance and if there isn’t just use that part.
But as I said I dont want a min distance
Something like this would work:
local ClosestMagnitude = math.huge()
local ClosestPart = nil
for _, Part in pairs(workspace.Parts:GetChildren()) do
if (Part.Position - <player>.Position).Magnitude < ClosestMagnitude then
ClosestMagnitude = (Part.Position - <player>.Position).Magnitude
ClosestPart = Part
end
end
so you just want to return the closest part
yes without any minDist variable
okay give me a second to the make the script
local BasePart:Part = workspace.Part -- Script checks all parts for the closest to this
local function ClosestPart()
local SelectedPart = {false, false}
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("Part") or v:IsA("BasePart") then
if v ~= BasePart then
local Formula = (BasePart.Position - v.Position)
local success, errormessage = pcall(function()
if Formula.Magnitude < SelectedPart[1] then
SelectedPart[1] = Formula.Magnitude
SelectedPart[2] = v
end
end)
if not success then
SelectedPart[1] = Formula.Magnitude
SelectedPart[2] = v
end
end
end
end
return SelectedPart
end
It will return a table {Distance, Part}
to access the part just do
ClosestPart()[2]
The reason i made it a table is just incase you need the magnitude later on
Its About what @Aviation_Joe said, you can iterate through every single object currently existing on the workspace
, and filter through them to find the exact closest.
local Players = game.Players
local function Magnitude(p)
return math.sqrt(p.X^2 + p.Y^2 + p.Z^2)
end
-- this function will give us the Distance of the Part
-- starting from the Origin to the parts Position
-- in order to find the Distance between parts, we subtract their positions
-- together, then check for Magnitude
-- This works becuase of the Pythagorean Theorem, the formula used
-- to get the Distance between points in math
-- The Forumula is simple, for 2d: (a² + b² = c²) for 3d: (a² + b² + c² = d²)
-- our job is to find the exact number of c (or d if 3D points), as c
-- will contain your distance we are looking for
-- if a² + b² will be c², what exactly is c?
-- once we have c², we then need to find the square root of c² to determine
-- c, the resultant will our Answer.
-- Vector3's will contain a .Magnitude function, so you can remove this if
-- you like, and replace it with .Magnitude
function FindNearestPartToPlayer(player: Player)
local currentDistance = math.huge
-- math.huge will allow us to look any distance to find these objects
-- if your game doesnt have very far away objects, you wont have to look very
-- far for them
local CurrentObject
-- CurrentObject will give us the last Object we found to be at a shorter
-- Distance.
local hrp = player.Character:FindFirstChild("HumanoidRootPart")
-- this will grab the center of the Player
for _,v in ipairs(workspace:GetDescendants()) do
-- Get Descendants will allow us to look through everything in the workspace
-- to find Parts, including Children of Objects
if not v:IsA("BasePart") then continue end
-- this is to skip the object if they are not a Part
-- BasePart is the class inherited by all Parts, so checking this will ensure
-- all Parts are given to us
if Players:GetPlayerFromCharacter(v.Parent) then continue end
-- this is to skip parts that are located in Players
-- removing this will end up giving you your own body parts as a result
local Distance = Magnitude(hrp.Position - v.Position)
-- Grabs the Distance between 2 parts
if Distance <= CurrentDistance then
-- if the Distance is less than the current one we have
--if it is, we will have a new object to look through
CurrentDistance = Distance -- Assigns new Distance
CurrentObject = v -- Assigns new Object
end
end
-- after this loop is done, you will likely end up with a Part
-- here it would be returned for usage
return CurrentObject
end
However, there are a couple of flaws with this, The Position of the Part will always be located at the Center of it, which if they are big parts, would contain a larger distance from the Player to the Part, so to fix that, you can probably Raycast Between them, and the exact location they Intersect at the Closest point to determine more accurate Results.
local player = game.Players.LocalPlayer
local character = player.Character
local function getDistance(part1, part2)
return (part1.Position - part2.Position).magnitude
end
local nearestPart
local nearestDistance = math.huge – Set to a large value initially
for _, part in ipairs(game.Workspace:GetDescendants()) do
if part:IsA(“BasePart”) then
local distance = getDistance(character.PrimaryPart, part)
if distance < nearestDistance then
nearestPart = part
nearestDistance = distance
end
end
end
print(“Nearest part:”, nearestPart.Name)
print(“Distance:”, nearestDistance)