I'm trying to make a UI that hovers over an NPC, so you always know where he is. I tried using Camera:WorldToScreenPoint for this. Now whenever the NPC is not on screen I would want the UI to be on the far left/right side of the screen, depending on which is closest to the NPC. However this is where my code fails and I don't know what to do.
Code:
while wait() do
local vector, onScreen = camera:WorldToScreenPoint(RexTorso.Position) --rex is the NPC
if onScreen then -- NPC is onscreen
script.Parent.Position = UDim2.new((vector.X-100)/mouseX, 0, (vector.Y-100)/mouseY,0) -- this works fine
else
if vector.X < 0 or vector.X < mouseX then -- NPC is on the left side of the view
-- this is where the issue is
script.Parent.Position = UDim2.new(0, 0, (vector.Y-100)/mouseY, 0)
else --right side of the view
script.Parent.Position = UDim2.new(1 - 200/mouseX, 0, (vector.Y-100)/mouseY, 0)
end
end
end
Thanks in advance!

2 comments
Could use billboard GUI and set to render on top of everything. As for when the character is off screen not sure how you could do that.
See that's what I thought of first too. But indeed you can't make it work when the NPC is off screen then. This code actually works fine, but I just don't know when to put it on the right or left side of the screen when the npc is off screen.