Optimal way to make BillboardGui when near closest object from a group of objects?

Hello developers,
I would like to make a BillboardGui that shows when the player gets to the nearest object (Adornee) from a bunch of them.

The issue is that I am not sure which way is most efficient to achieve this.

My most comfortable approach would be to loop through the objects in RenderStepped, find the closest one, and set its Adornee. But wouldn’t that heavily affect the performance? (due to calculations and looping every frame)

Another way would be to make a Billboard GUI to each of the objects, but that would not accept user input, nor would be optimal.

Here’s some pseudo-code to give you an idea of what my first approach would look like:

local RunService = get RunService
local groupOfParts = Path to parts

RunService.RenderStepped -> (function()
    local lastPart = nil
    for part in groupOfParts do
        part_distance = (calculated part distance from player)
        lastPart_distance = (calculated lastPart distance from player)
        if(part_distance < lastPart_distance) then
            lastPart = part
        lastPart = part
    billboard.Adornee = lastPart

What would be the optimal and correct way to do such a thing? Or is my first approach not performance-killing and fine to use?

Thanks in advance.

first, using Stepped or Heartbeat does almost the same as Renderstepped, but doesnt affect the framerate

and for me, I’d work with a single connection to the event to handle every single billboardgui in the same thread

RenderStepped runs just before the frame, which is why I use it (for the billboard GUI to show up), but you’re right, all options are not that different in terms of running times.

I did not mean FPS when mentioned performance, but more… Lag and slowness on the client’s behalf.

Pardon me, I am not sure I understand what you mean, I currently have only one billboard. Could you please elaborate/explain?

Thanks for answering :pray:

You could just update the billboard whenever the player moves instead of having it run every frame. It doesn’t make sense to loop through everything to check the distances if the player hasn’t moved at all.

Aside from that, I don’t think this would be that performance heavy. It depends how many parts you’re looping through to check the magnitude.

I didnt meant visual frames per second
I meant processing frames

also, the difference between making the frame appear on different parts of the frame is negligible visually
if you connect to Stepped, then after you get in range, you’ll miss the next render step, then the gui will be visibile, then the next render step will display it. that’s like 0.06 seconds
if you connect to Heartbeat, after you get in range, you’ll enable the gui, and then the renderstep will display it
if you connect to Renderstepped, after you get in range, renderstepped would slow down the frame to check if you’re in range and both enable, then display it

and the second part doesnt matter if you have only one billboard

1 Like

Interesting approach! I wonder if Humanoid.Running is reliable enough for that.

Will attempt, thanks!

The amount is dynamic. Could be there, could be 60.

It depends on how many objects the player had placed. But I would assume it would be at most 20.

I see now. Thank you!