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
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
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.
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
soo magnitude is the difference between positions.
XDifference = PartX-PlayerX
and so on
and the formula for magnitude is this
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