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)
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
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
@2112Jayto 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)
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 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)
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.