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.
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.
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
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
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
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.
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
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.
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).