UI Positioning isnt correct on WorldObject Position

I don’t know how to explain so I’ll just give an example:
You got a Part which every 24 seconds. It create a Image Button on a player screen on the exact location of the part. Since I haven’t learn about UI much. I don’t know how to do it. The only thing I can do is to find a topic and I haven’t found any thing related to what I want.

If you can help me It’ll be great!

Thanks for reading! :slight_smile:

https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToScreenPoint

This is relevant however what if the part is no longer contained inside of the player’s camera?

1 Like

It a vector 3 value and I dont know how to convert it into Udim2.fromscale()
Also, I tried it out already!

It’s a Vector3 but the X and Y components describe exactly what you need, offset in pixels from the top left hand corner. It’s not a Vector3 value describing the position in the worldspace.

1 Like

I made a debugger and it print out this:
VectorX:-0.33457773923874 VectorY:1.2935553789139

Another value i get from the imagebutton it self:
{-0.903, 0},{2.051, 0}
It was out of the Screen (I used FromScale)

    local camera = workspace.CurrentCamera
    local worldPoint = Vector3.new(0, 10, 0)
    local vector, onScreen = camera:WorldToScreenPoint(worldPoint)
     
    local screenPoint = Vector2.new(vector.X, vector.Y)
    local depth = vector.Z

This is from the documentation page.

1 Like

In conclusion of it, It doesnt work because the Y value is greater than 1 (so it out of the screen)

All of the scripts are corrected but i dont know what happened to it

local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local camera = workspace.CurrentCamera

task.wait(5)

local wp = part.Position
local vector, onScreen = camera:WorldToScreenPoint(wp)

for _, player in ipairs(players:GetPlayers()) do
	local frame = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Frame")
	frame.Position = UDim2.new(vector.X, 0, vector.Y, 0)
end

This worked on my end.

1 Like

What happens if you added only .fromscale()?

Yeah, that would work in exactly the same way as I’m only using the scale components when creating the UDim2 value.

local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local camera = workspace.CurrentCamera

task.wait(5)

local wp = part.Position
local vector, onScreen = camera:WorldToScreenPoint(wp)

for _, player in ipairs(players:GetPlayers()) do
	local frame = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Frame")
	frame.Position = UDim2.fromScale(vector.X, vector.Y)
end

VectorX:2.0894441604614 VectorY:-0.99359226226807
Result

local newvector,insight = workspace.Camera:WorldToScreenPoint(object.PrimaryPart.Position)

print(VectorX:..newvector.X.. VectorY:..newvector.Y)
CreateSun(newvector.X,newvector.Y)

the X Value is still out of the frame

I forgot my Sun Generation function too!:

function CreateSun(x,y)
	local sun = game.Lighting.SunIcon:Clone()
	sun.Position = UDim2.new(x,0,y,0)
	sun.Parent = script.Parent
local players = game:GetService("Players")
local part = workspace:WaitForChild("Part")
local camera = workspace.CurrentCamera

task.wait(5)

local wp = part.Position
local vector, onScreen = camera:WorldToScreenPoint(wp)

for _, player in ipairs(players:GetPlayers()) do
	local frame = player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("Frame")
	frame.Position = UDim2.new(vector.X + frame.Size.X.Scale/2, frame.Size.X.Offset/2, vector.Y + frame.Size.Y.Scale/2, frame.Size.Y.Offset/2)
end

This takes into account the size of the frame (in your case button) and places it so the center of the frame should match the center of the part.

https://gyazo.com/40b9d5458033fe951b63724629f72e32

1 Like
function CreateSun(x,y)
	local sun = game.Lighting.SunIcon:Clone()
	sun.Position = UDim2.new(x + sun.Size.X.Scale/2, sun.Size.X.Offset/2, y + sun.Size.Y.Scale/2, sun.Size.Y.Offset/2)
	print(sun.Position)
	sun.Parent = script.Parent

Is this code correct because It was out of the box again


This is the gameplay, the imagebutton should spawn on the sun correctly

It’s probably ending up out of the box because the specified worldpoint is out of view, use the second value returned by “WorldToScreenPoint()” to determine whether or not the worldpoint is currently in the camera’s viewport.

1 Like

It prints out false so it isnt in a point of view

Right, you don’t want the button to be cloned if wherever the button is being placed isn’t currently in the viewport of the camera (otherwise it’ll be positioned off-screen).

Ok so I’ve plan to change the spawner to the top of it’s head
C:\Users\Admin\Documents\RoblosStudio\PVZ lol.rbxl - Roblox Studio - Gyazo