How do i find the closest object to a player?

I have multiple parts called “JumpTarget”. when the player jumps, the script should detect the magnitude between the plr and the part, issue is, i don’t know how to make the script detect the CLOSEST part.

how would i detect which one is actually closest to the player? what would i need to use?

i already have the magnitude detection working, i just need to know how to get the closest part

i have marked this as resolved, but there are mixed responses.

someone suggested the use of WorldRoot | Roblox Creator Documentation
you may want to use this as i’ve seen it on the devhub and it does seem more ‘practical’ than other methods but not up to me

1 Like

Put the parts you want to find the closes of. Run a loop of the parts. And take magnitude and compared

Should look something like this
This function will return a table. table[1] being the part, table[2] being the distance from character to the part

function findClosestPart(PartsFolder,HumanoidRootPart)
    local partsTable = partsFolder:GetChildren()
    local closest = {nil,math.huge}--Part, distance
-------------Loop thru
    for i,v in pairs(partsTable) do
        local distance = (HumanoidRootPart.Position - v.Position).magnitude
        if distance < closest[2] then
            closest = {v, distance}
        end
    end
-------------Done loop return closest
    return closest 
end
4 Likes

i’m confused on how this works… is it ok if you explain how this works? (as in the script itself, not how it’s used)

also where would the two tables be defined? i’m trying to print their contents figured it out

after some editing:
this does find the distance from the part, yes but for some reason it’s not actually detecting the closest part. sometimes it says JumpTarget and other times it has JumpTarget2 despite JumpTarget being further away from spawn

also if i walk up to a new target it doesn’t change. i’m assuming this is because it only detects it once but again this is under a function that i made to fire every time the humanoid state changes so idk what’s going on there

The HumanoidStateType doesn’t change regularly, use position of the character itself. Unless you’re jumping and in that case the original script isn’t working as intended.

You could use WorldRoot | Roblox Creator Documentation or WorldRoot | Roblox Creator Documentation to detect parts in a nearby radius instead of whatever the hell the other solutions people gave here are.

Could also use magnitude checks while looping thru the table it returns if just the radius is not enough for you.

1 Like

I’ll try this method along with the other one to see which one works better in my scenario, thank you

i removed the

return closest

and it worked, but it made an issue where the script was reading all the parts instead of the closest. i changed the

local closest = {nil,math.huge}--Part, distance

to

local closest = {nil,50}--Part, distance

and this fixed everything

local distanceTable = {}

local function distances()
	for i, v in pairs(workspace.Model:GetChildren()) do --change this line
		local distanceFromPart = (humanoidRootPart.Position - v.Position).magnitude
		distanceTable[v.Name] = distanceFromPart
	end

	table.sort(distanceTable, function(ele1, ele2)
		return ele1[2] > ele2[2]
	end)

	for i, v in pairs(distanceTable) do
		print("Your distance from part "..i.." is "..v..".")
	end
end

That will only work now if the closest part is less than 50 studs away.

this is intended, i want the player to be within a certain range before it detects parts

soo magnitude is the difference between positions.

XDifference = PartX-PlayerX

and so on
and the formula for magnitude is this

image

If the difference is negative it will flip it to a positive. Basically triangulation and Pythagorean theorem but with a extra axis. Roblox has a magnitude function built in so you don’t gotta do that formula.

going partsFolder:Children() gets all children in the object, in this case a folder with all the jump teleport pads.
for i,v in pairs(table) do loop through the table. I being the key of the table, and v being the part
then it compares the part distance from player. If it’s lower than the current closest once which starts off as nil, with a math.huge distance. Then it set’s it as the closest. It will loop and constantly change closest.

Once the loop is done it returns the closest table. with table[1] being the part and table[2] being the distance

2 Likes