How to to make closest part to the middle of screen show Gui

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So i want to make it so that the closest part to the middle of screen (or at least the nearest to the mouse) to show a Gui. also i plan to get the finished game to be an FPS

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that i dont really have a clue on how to make it. even after surfing the internet a bit

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

the solutions i have tried is searching around the internet as i have said before.
I have also tried finding the closest part to the player and then changing the player to the mouse and i have still failed to do even that

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


These are the four parts. i put them in workspace.Model.Folder
and inside the part its: part.BillboardGui.ImageLabel

i really want this to work so any help is appreciated :smiley:

1 Like

In order to find the part closest to the screen, you could use WorldToScreenPoint, to convert the .Positions of your parts to points on the screen. Then you could compare the result of that function, to the midpoint of the screen in pixels( which I believe can be obtained by getting the AbsoluteSize of a ScreenGui, and dividing it by 2 ). The comparison can be done by reading the .Magnitude of one Vector2 subtracted from another. Then you can just use some basic if statements and loop, to find the smallest magnitude value, which tells you what the closest part to the center of the screen is( at least from a on screen perspective ).

To find what is closer to the cursor, you can either do the same as above, but replace the screen center Vector2 with the X and Y coordinance in pixels of the Mouse, or compare the 3d .Positions of your parts, to the Mouse.Hit.p, again by using .Magnitude.

Hopefully this is helpful! Tell me if something doesn’t make sense.

2 Likes

This is very helpful but can you please simplify it if you can?
If you cant then can you please tell me what i should learn before doing this.
thanks :+1:

Sorry that that sounded so complex in a jumble like that. Let me craft you a example.

Camera = workspace.CurrentCamera
Part = workspace.Part
ScreenGui = script.Parent --this is assuming the LocalScript's parent is a ScreenGui

local worldPoint = Part.Position
local vector, onScreen = Camera:WorldToScreenPoint(worldPoint)

 if onScreen == true then
    local partScreenPoint = Vector2.new(vector.X, vector.Y)
    local midScreenPoint = ScreenGui.AbsoluteSize / 2

    local PixelMagnitude = (partScreenPoint - midScreenPoint).Magnitude --this is the distance in pixels that the part is from the center of the screen
end

You can use this to compare the PixelMagnitudes of each of the parts, to find the smallest one. The smallest one should be the part closest to the center of the screen.

And here is the mouse example( method 2 ):

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Part = workspace.Part

partPosition = Part.Position
mousePostion = Mouse.Hit.p -- this only works if the mouse is hovering over a object ( you would want to use the X and Y 2d positions of the mouse, and the first example, if this wouldn't work )
studMagnitude = (partPosition - mousePosition).Magnitude --this time the studMagnitude equals the magnitude in the measurement of studs, and we are working with Vector3s not Vector2s

Hopefully this clears things up. I wrote this all on mobile, so I might have made a typo here or there. It’s just to demonstrate what I was trying to explain.

Here are a few things that I used in the code that you should look into:

3 Likes