Camera function gives wrong result When in viewport

I am using this function that behaves exactly like Camera:WorldToScreenPoint() but is compatible with viewports

function WorldToScreen(Creature , at)
	local viewportFrame = Creature.Adornee.Parent.Parent
	local viewportCamera = viewportFrame.CurrentCamera
	local point = viewportCamera.CoordinateFrame:pointToObjectSpace(at)
	local aspectRatio = viewportFrame.AbsoluteSize.X/viewportFrame.AbsoluteSize.Y
	local hfactor = math.tan(math.rad(viewportCamera.FieldOfView)/2)
	local wfactor = aspectRatio*hfactor
	--
	local x = (point.x/point.z) / -wfactor
	local y = (point.y/point.z) /  hfactor
	--
	return Vector2.new(viewportFrame.AbsoluteSize.X*(0.5 + 0.5*x), viewportFrame.AbsoluteSize.Y*(0.5 + 0.5*y))
end

The problem is that this does not return a correct result, always being notably offset, i don’t want this to happen, how can i make this function give an accurate result.