Help about getting the closest part from a specific part

Hello everyone. This is my first post.
I am trying to make a bus passenger board. I want it to show some string values in the next stop (picture below)
Snapshot_24-01-16_16-25-50
You can see in the Bus Stop model there is a folder called 261.
What I need help about is making a script that gets that closest bus stop with the folder 261, I know in which direction to look (magnitude, position, for i,v) but I am fairly new with that so I don’t know how to exactly achieve what I am trying to do.
Any help is appreciated, thanks.

EDIT
I managed to figure out something, but I still need help about how to get the closest one from the list.

local stopList = {}
local valid = false
local nr = script.Parent.Route

function UpdateStatus()
	stopList = {}
	for i,v in pairs(workspace:GetDescendants()) do -- get all stops
		if v.Name == "BusStopBody" then
			table.insert(stopList,1,v)
			break
		end
	end

	for i,v in pairs(stopList) do -- check if route passes through here, if not remove
		valid = false
		if v:FindFirstChild(nr.Value) then --here the nr.Value is 261, just like the folder name so it should work
		--how to get which one is the closest from stopList here?

		end
	end
end

Explain this better … I only see the one folder 261. There isn’t anything to reference on distance or other folders.

1 Like

You see the part called PassengerDisplay?
I need help about a script that gets the closest Bus Stop from that part “PassengerDisplay”

@2112Jay updated, so if you could check the post now that would be awesome.

I’m still lost what you’re looking to do here …

i made 5 parts on the workspace part0 to part4 … this then finds the one closest to part0
You’ll have to modify it to work for you.

local targetPart = workspace.part0
local targetPartName = "part0"
local targetPart = workspace:FindFirstChild(targetPartName)

local function getDistance(part1, part2)
	return (part1.Position - part2.Position).Magnitude
end

local partNames = {"part1", "part2", "part3", "part4"}
local closestPart = workspace[partNames[1]]
local minDistance = getDistance(targetPart, closestPart)

for _, partName in ipairs(partNames) do
	local part = workspace:FindFirstChild(partName)
	if part then
		local distance = getDistance(targetPart, part)
		if distance < minDistance then
			minDistance = distance
			closestPart = part
		end
	end
end

print("Closest part to the specified part:", closestPart)

Think the answer to your overall question is: Magnitude

1 Like

@2112Jay to clarify: I want to get the closest part with the name “BusStopBody” and get the instances inside of it. But keep in mind that there are several parts named BusStopBody, so we need to get the closest one and access it, that is what I’m looking for. Take a look on the script I did. That’s what I need, how to get the closest one from that list. (check the script I posted under section EDIT: )

Could you do something that would check each of the parts position and compare it to the bus current position?
Like, get all the parts you want to check, check their current positions, compare it to the bus’s current position, which ever is closest will be that? (Not sure if this would work just a guess)

I showed you a way to get Magnitude. You can get it from here …

1 Like

Check the script, I made the stopList, which has every stop, now the question is which one is the closest from the list, I appreciate your comment, but I don’t think I would know how to do that.

I’m not sure how to do it for the list, could you perhaps edit it, thanks anyway

I looked and found this page that is someone finding closest part to a player, you could try looking in that to see if there is anything that could show you how
(edit: also you can just search “roblox studio find closest part to another part” or something, which is how i found that)

One more shot at it …

local stopList = {}
local valid = false
local nr = script.Parent.Route
local player = game.Players.LocalPlayer

function UpdateStatus()
    stopList = {}
    for i, v in pairs(workspace:GetDescendants()) do
        if v.Name == "BusStopBody" then
            table.insert(stopList, 1, v)
            break
        end
    end

    local closestStop = nil
    local minDistance = math.huge

    for i, v in pairs(stopList) do
        valid = false
        if v:FindFirstChild(nr.Value) then
            local distance = (v.Position - player.Character.HumanoidRootPart.Position).Magnitude
            if distance < minDistance then
                minDistance = distance
                closestStop = v
            end
        end
    end

   return closestStop
end

print(UpdateStatus())

Tried to use your script here. Nothing has been tested.

1 Like

You are a certified legend, you earned a new follow. edited only this:

(v.Position - player.Character.HumanoidRootPart.Position) to (v.Position - script.Parent.Position)

Good deal glad you figured it out.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.